I'm trying to display a rather big image in a UIImageView. The image downloads correctly (using a NSSessionDownloadTask) and I can create a UIImage from it, however, nothing is displayed in the UIImageView. I don't get any memory warnings or errors. Anybody knows what's going on?
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
self.imageView.image = image;
self.progressView.progress = 0.0f;
[self.activityView stopAnimating];
[self.activityView setHidden:YES];
NSLog(@"Received full Image!"];
}
Your image just crashed the Chrome tab I tried to display it in on a 4GB laptop. It also caused my laptop to sputter quite a bit before it was saved.
It is a 36MB jpeg compressed bitmap. 18K x 18K pixels. Let's do some math.
Given the resolution, you have 324000000 pixels in all. 324M pixels. Each represented by 3 bytes of color info in memory. A good approximation would be 1GB of raw pixel data in all then. And I am not sure that takes into account how a UIImage instance stores that internally.
Given the above and the fact that the most recent (and powerful) iOS devices have 1GB of RAM in total, I think it would be safe to say that the image is too big even if there is not inherent size limit invovled.
I think you are trying to set your imageView content from a background thread. Any UI update must come for your main thread (main queue) in order to be safe. Try to set the UIImageView image property on your main queue, by doing the following:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
self.progressView.progress = 0.0f;
[self.activityView stopAnimating];
[self.activityView setHidden:YES];
});
NSLog(@"Received full Image!"];
}
EDIT: What I wrote above remains true, but as dandan78 pointed out your image is quite too big and the solution above is not likely to fix your problem since you can't overcome the de-facto memory limitations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With