GrayFox
GrayFox

Reputation: 121

CGImageRef Memory leak after release and autoreleasepool

Im trying to implement show image from local content but for some reason memory Would not get freed.

@autoreleasepool {
  ALAssetRepresentation *rep = [myasset defaultRepresentation];
  CGImageRef iref = [rep fullResolutionImage];
  UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[UIScreen mainScreen].scale orientation:(UIImageOrientation)rep.orientation];
  CFRelease(iref);
  self.imageView.image = largeimage;
  largeimage = nil;
 }

As suggested ,i am used

CGImageRelease(imageRef);

but still i am got an memory leak. After that i am wrap code with an

@autoreleasepool {}

block but that also not solve my problem. What chould I do ?

Upvotes: 2

Views: 100

Answers (1)

Arun
Arun

Reputation: 1411

I think the issue is when you assign the image to your image view. Can you try resizing the image before assigning it to image view?

Use this method

- (UIImage *)resizeImage:(UIImage *)sourceImage toSize:(CGSize)newSize
{
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [sourceImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Upvotes: 2

Related Questions