Question

I am trying to implement a Coverflow ( iCarousel ) with AsyncImageView ( https://github.com/nicklockwood/AsyncImageView ) as cover. It works well with images of size upto 4 MB. But the app crashes when trying to load images of size more than 10 MB. My question is -

1 ) Can i load a image of size 10 MB without tiling ? Since the source of image is from device camera, is it possible to tile those images and then load those images. If so can you share some ideas/code blocks on tiling a large image ?

P.s : I have tried compressing the images, by using UIImageJPEGRepresentation(image,scale), although the image size got reduced from 10 MB to 100 KB, but when i try to load the compressed images, memory issues shows up again. (Looks like iOS decompresses to some extent)

Was it helpful?

Solution

You are confused with the dimensions of the image and its size on disk.

In memory an image take WIDTH*HEIGHT*4 pixels, so let's say your image is 1000x1000px you end up using 4Mb ram.

UIImageJPEGRepresentation save the image with a compression factor, so you end up with a smaller image on disk, but the image has still the same dimensions.

To solve your problem you need to scale the image you downloaded to the correct dimension for your coverflow item.

You can do this using ImageIO framework :

  • Create a CGImageSource from the downloaded data
  • Call CGImageSourceCreateThumbnailAtIndex with the 2 properties kCGImageSourceCreateThumbnailFromImageIfAbsent and kCGImageSourceThumbnailMaxPixelSize

OTHER TIPS

Here is the working code

   UIImage *result = nil;
if ([data length]) { // NSData of the image


    CGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef)data, nil);

    NSMutableDictionary *options = [NSMutableDictionary dictionary];

    [options setObject:(id)kCFBooleanTrue forKey:(id)kCGImageSourceCreateThumbnailFromImageIfAbsent];
    [options setObject:[NSNumber numberWithInt:400] forKey:(id)kCGImageSourceThumbnailMaxPixelSize];
    CGImageRef imageRef = CGImageSourceCreateThumbnailAtIndex(sourceRef, 0, (CFDictionaryRef)options);

    if (imageRef) {
        result = [UIImage imageWithCGImage:imageRef]; //Resulting image
        CGImageRelease(imageRef);
    }

    if (sourceRef) CFRelease(sourceRef);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top