IPhone Multi-Image Application

I am new to iPhone app development, so I’m probably doing something wrong.

Basically, I download a bunch of images from the Internet and then crop them. I managed to find examples of loading images asynchronously and adding them to views. I was able to do this by adding an image with NSDatathrough NSOperationwhich was added to NSOperationQueue.

Then, since I had to make thumbs of a fixed size, I needed a way to crop these images, so I found a script on a network that mainly uses it UIGraphicsBeginImageContext(), UIGraphicsGetImageFromCurrentImageContext()and a UIGraphicsEndImageContext()cropped image for drawing, as well as unimportant size calculations , to draw.

The fact is that this method works, but since it generates 20 such images, it accidentally crashes after some of them have been generated, or sometimes after closing and reopening the application once or twice.

What to do in this case? I also tried to run these methods asynchronous, with NSOperationsand NSOperationQueue, but no luck.

If the lesson code is more appropriate than I think, here it is:

UIGraphicsBeginImageContext(CGSizeMake(50, 50));
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = CGPointMake(0.0,0.0); //this is actually generated
                                             // based on the sourceImage size
thumbnailRect.size.width  = 50;
thumbnailRect.size.height = 50;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();

Thanks!

+3
source share
3 answers

The code for scaling images looks too simple. Here is the one I use. As you can see, there are no leaks, objects are freed when they are no longer needed. Hope this helps.

    // Draw the image into a pixelsWide x pixelsHigh bitmap and use that bitmap to 
// create a new UIImage 
- (UIImage *) createImage: (CGImageRef) image width: (int) pixelWidth height: (int) pixelHeight
{ 
    // Set the size of the output image 
    CGRect aRect = CGRectMake(0.0f, 0.0f, pixelWidth, pixelHeight); 
    // Create a bitmap context to store the new thumbnail 
    CGContextRef context = MyCreateBitmapContext(pixelWidth, pixelHeight); 
    // Clear the context and draw the image into the rectangle 
    CGContextClearRect(context, aRect); 
    CGContextDrawImage(context, aRect, image); 
    // Return a UIImage populated with the new resized image 
    CGImageRef myRef = CGBitmapContextCreateImage (context); 

    UIImage *img = [UIImage imageWithCGImage:myRef];

    free(CGBitmapContextGetData(context)); 
    CGContextRelease(context);
    CGImageRelease(myRef);

    return img; 
} 


// MyCreateBitmapContext: Source based on Apple Sample Code
CGContextRef MyCreateBitmapContext (int pixelsWide,
                                    int pixelsHigh)
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    if (context== NULL)
    {
        free (bitmapData);
        CGColorSpaceRelease( colorSpace );
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );

    return context;
}
+5

, (, UIGraphicsBeginImageContext) UIKit, .

, .

+2

Sounds suspicious, like due to a memory failure. Launch the Leaks tool and view general trends in memory.

0
source

Source: https://habr.com/ru/post/1711265/


All Articles