0

In my iPad app at one moment I load 100 images from Photo Stream using ALAsset and following code:

  ALAsset *asset = [assets objectAtIndex:[sender tag]];
  ALAssetRepresentation *representation = [asset defaultRepresentation];
  UIImage *image = [[UIImage alloc] initWithCGImage:[representation fullScreenImage]
                                                     scale:1.0f
                                               orientation:0];

And everything works perfectly. But when I cache it to the file system as JPEG files, and then load them again with UIImage *image = [UIImage imageWithContentsOfFile:fullPath], application crashes with Memory Warning, and I can see in the profiler that it really uses a lot of RAM. Why is this happening?

kavalerov
  • 390
  • 3
  • 13
  • Use @autorelease to release the memory in a loop. – IamAnil May 30 '14 at 11:05
  • Similar to below code : - (void)useALoadOfNumbers { for (int j = 0; j < 10000; ++j) { @autoreleasepool { for (int i = 0; i < 10000; ++i) { NSNumber *number = [NSNumber numberWithInt:(i+j)]; NSLog(@"number = %p", number); } } } } – IamAnil May 30 '14 at 11:06
  • I am trying this right now. I have tried to use it in different place, and it didn't work out. – kavalerov May 30 '14 at 11:07
  • more info : http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc – IamAnil May 30 '14 at 11:08
  • Well, the problem there is that I don't have actual loop right there. I know about @autoreleasepool and trying to use it in different manner right now. – kavalerov May 30 '14 at 11:09
  • And it is still crashing with: Terminated due to Memory Error – kavalerov May 30 '14 at 11:10
  • can you post more code... Like .h and .m so that we can analyse it properly and give you the answer . – IamAnil May 30 '14 at 11:11
  • Images, when expanded as in a UIImage, take an *enormous* amount of storage. If you *must* keep an in-storage copy, keep a copy of the JPEG file as NSData, don't keep the UIImage. – Hot Licks May 30 '14 at 11:16
  • @HotLicks Even if I am using them in UIImageViews? Because I not just loading the images, but displaying them as well. – kavalerov May 30 '14 at 11:17
  • When you go to display a given image, *then* you convert it to a UIImage, but only while it's on-screen. – Hot Licks May 30 '14 at 11:27
  • well, They have to be all on screen at the same time. And it works perfectly, when I use ALAsset, and it crashes, when I load files from the file system. – kavalerov May 30 '14 at 11:29
  • @flybirdx This may help. Use UIImage+Resize category and shrink your pics down when showing preview - http://stackoverflow.com/questions/12773074/received-memory-warning-on-setimage – Sam B May 30 '14 at 12:19
  • I already use this category, but the problem is that this is not showing the preview of the image, i have to have them all in 2048X1526 size. – kavalerov May 30 '14 at 13:16
  • The most weird thing is that there is no problem using ALAsset, and this happens ONLY when I load from the files – kavalerov May 30 '14 at 13:27
  • The full-sized images won't even all fit on the screen at the same time. For thumbnails use a resized image. – Hot Licks May 30 '14 at 14:20
  • I am sorry, but this is really not applicable in my case. And it still leaves the question open - why uiimageview takes so much more memory if loaded from file. – kavalerov May 30 '14 at 16:28

2 Answers2

1

One thing,

When you are loading the image from gallery, why not store the AssetURL, instead of the UIImage; that should take less space and increase speed.

When you need to show, use the thumbnail representation, perhaps?

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
Debanjan
  • 99
  • 9
0

Ok, it was my bad. I found the problem, and it comes out that I have memory problems in any case. ALAsset and imageWithContentsOfFile are working exactly the same.

Now I will try to find the way to reduce each image's size.

kavalerov
  • 390
  • 3
  • 13