3

I set a image with 10000 * 10000 pixels to UIImageView from network by SDWebImage, and App crashed because it allocated too much memory. I tried to resize the image that had been loaded by SDWebImage, so I add the code below:

UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, CGRectMake(0, 0, size.width, size.height));
CGContextSetInterpolationQuality(context, 0.8);
[self drawInRect:drawRect blendMode:kCGBlendModeNormal alpha:1];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Although the image size was smaller, my app crashed due to the same reason. It seems that there are some rendering action during the resizing action, the memory would rose to 600M and fell to 87M in a little while.

How can I resize a image without rendering?

It seems that display the image by UIWebView did not exist the problem. How it works?

Any help and suggestions will be highly appreciable。


Resolution: https://developer.apple.com/library/ios/samplecode/LargeImageDownsizing/


The Resolution does work for jpg but not for png

Community
  • 1
  • 1
tripleCC
  • 39
  • 8
  • This might help: http://stackoverflow.com/questions/24338414/didrecievememorywarning-called-after-viewdidappear-when-displaying-large-image-f – arc4randall Mar 16 '16 at 19:57

2 Answers2

1

You can't unpack the image into memory because it's too big. This is what image tiling is for, so you would download a set of tiles for the image based on the part you're currently looking at (the zoom position and scale).

I.e. if you're looking at the whole image you get 1 tile which is zoomed out and therefore low quality and small size. As you zoom in you get back other small size images which show less of the image 'area'.

The web view is likely using the image format to download a relatively small image size that is a scaled down version of the whole image, so it doesn't need to unpack the whole image to memory. It can do this because it knows your image is 10,000x10,000 but that it is going to be displayed on the page at 300x300 (for example).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks. Do you have any demo for it? I use pod to manager my project, so I can't edit source code of SDWebImage, I have to get the image from it and do extra operation. – tripleCC Mar 16 '16 at 12:57
  • tiling is generally a server supported operation, you need to be able to request the individual tiles (a 3rd party library can manage it). Alternatively you could have another service generate tiles if required, or generate a smaller version of the image. – Wain Mar 16 '16 at 13:23
  • Also see http://stackoverflow.com/questions/14772913/load-uiimage-low-quality if you want to lower quality instead of tile. – Wain Mar 16 '16 at 13:24
0

Did you try to use : UIImageJPEGRepresentation (or UIImagePNGRepresentation)

You can make your image size smaller with it.

Nhat Dinh
  • 3,378
  • 4
  • 35
  • 51
  • 1
    Please show us exactly how UIImageJPEGRepresentation or UIImagePNGRepresentation applies to this case. – El Tomato Mar 16 '16 at 11:12
  • @Dinh Nhat, I had tried this function yet when I was involved the problem. But it seems that this function would also occupy too much memory. – tripleCC Mar 16 '16 at 12:38