Vincent Yiu
Vincent Yiu

Reputation: 1291

CGImageRelease causing crash

I am using AGImagePickerController to pick multiple pictures from album, and then push the selected assets to a viewController where it tries to convert each asset into an UIImage. However, I found out that if I selected more than 20 images, I will start to get memory low warning and the app exited. Here is my code of the conversion

for(int i =0 ; i < [self.selectedPictures count] ; i++)
{
    NSLog(@"Object %d",i);
    ALAsset *asset = [self.selectedPictures objectAtIndex:i];
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    UIImage *anImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
    float newHeight = anImage.size.height / (anImage.size.width / 1280);

    UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(newHeight, 1280.f) interpolationQuality:kCGInterpolationHigh];

    UIImage *resizedThumbnailImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:CGSizeMake(290.0f, 300.f) interpolationQuality:kCGInterpolationHigh];

    // JPEG to decrease file size and enable faster uploads & downloads
    NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.6f);
    //NSData *thumbnailImageData = UIImagePNGRepresentation(thumbnailImage);
    NSData *thumbnailImageData = UIImageJPEGRepresentation(resizedThumbnailImage, 0.6f);


    PFFile *photoFile = [PFFile fileWithData:imageData];
    PFFile *thumbnailFile = [PFFile fileWithData:thumbnailImageData];

    [photoFile saveinbackground];
    [thumbnailFile saveinbackground];
}

So i figured out that I should add CGImageRelease(iref); after anImage to release the iref, and the memory warning is gone. However, my app will crash after the last asset is converted to UIImage. And so far i could not find out why it is crashing.

Upvotes: 1

Views: 1220

Answers (2)

Vincent Yiu
Vincent Yiu

Reputation: 1291

I found a way to fix this. use @autoreleasepool

Upvotes: -1

iDev
iDev

Reputation: 23278

You shouldn't be doing CGImageRelease(iref); unless you use CGImageCreate, CGImageCreateCopy or CGImageRetain. That is the reason why it is crashing.

Upvotes: 3

Related Questions