Segev
Segev

Reputation: 19303

Loading and saving large images using UIImageJPEGRepresentation crashes my app

My app can take pictures or select them from the camera roll, the, using UIImagePicker I send the picture to a ViewController where the user then can crop the image.

The final result is UITableView with a name and a thumbnail and every row leads to the full sized picture. The thumbnails are the original picture in a different scale.

This is how I save my picture after the crop:

[UIImageJPEGRepresentation(_justNowImage.image, 0.1) writeToFile:jpgPath atomically:NO];

This is how I open it to a thumbnail or to full size.

_imageInPocket.image = [UIImage imageWithContentsOfFile:jpgPath];

If I crop small pictures everything is working fine but I try to crop a big picture (over 2 MB) the app will crash, probably du to lack of memory.

My project is using ARC. Do I still need to release the images from the memory somehow? Should i create small versions of the images and use them as thumbnails instead of loading all the original pictures in the UITableView and change their scale to fit the Cell ?

Upvotes: 1

Views: 1164

Answers (1)

Arthur
Arthur

Reputation: 1760

Maybe you should load the data and convert it to an image using

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSURL *url = [NSURL URLWithString:@"Your URL"];
NSURLRequest *theRequest = [NSURLRequest
    requestWithURL: url
    cachePolicy: NSURLRequestUseProtocolCachePolicy
    timeoutInterval: 60.0
];

When you use this method, you will get your image in packets. After this you should call and gather all packets

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
 [imageData appendData:data];
}

After this you need to convert the data into an image and close the connection

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  UIImage *image = [UIImage imageWithData:imageData];
  imageView.image = image;
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

Upvotes: 1

Related Questions