Question

I wish to enumerate over my album, but when i test the app on my device, the app crashes after 30 seconds. when I used Instrument, i discovered that ARC doesn't autorelease the assets, so every time it deals with an image, it allocates the image size. My main goal is showing only device's screenshots. so lets say i have 1000 photos, after 30 images i get 30MB allocation, which causes the crash. How do I explicitly release the memory?

Here is my code:

[self.assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

    if (result) {

        ALAssetRepresentation* representation = [result defaultRepresentation];

        // Retrieve the image orientation from the ALAsset
        UIImageOrientation orientation = UIImageOrientationUp;

        NSNumber* orientationValue = [result valueForProperty:@"ALAssetPropertyOrientation"];

        if (orientationValue != nil) {
            orientation = [orientationValue intValue];
        }

        CGFloat scale  = 1;
        UIImage* image = [UIImage imageWithCGImage:[representation fullResolutionImage]
                                             scale:scale orientation:orientation];

        if ( image.size.height/2==screenHeight && image.size.width/2==screenWidth)
            [self.assets addObject:result];
    }
}];

I think default representation causes the malloc.

Thanks in advance...

Was it helpful?

Solution

You've discovered that UIImage objects can take up a lot of memory when they're created.

Instead of creating UIImage objects, why not use API's that ALAsset provides to you that don't hog memory. For example, you can get the image dimensions (I've linked the Apple documentation for you).

Also, this line:

if ( image.size.height/2==screenHeight && image.size.width/2==screenWidth)

seems a little dangerous to me. Unless you control all the images that are being loaded, any full resolution image is very likely to be a different size than exactly 1/2 of the screen height and width.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top