1

I'm working on a carrousel of large amount of big images and I am making some tests trying to improve the performance loading the images. Right now, even if I am already decompressing the jpg on a different queue, it's still taking a little bit, mostly comparing with the photo album app which is included in the iOS. Furthermore, if I pass the images very fast, I can produce memoryWarnings.

So what I am trying to do is to store the CGImageRef (or the UIImage already decompressed: raw data) into Core Data. But all the answers and options I found are using UIImageJPegRepresentation, but doing that I would compress the image again, wouldn't I?

Anybody knows if there is a way? Am I focusing the problem wrongly?

crisisGriega
  • 852
  • 7
  • 17
  • The second answer [here](http://stackoverflow.com/questions/6073259/getting-rgb-pixel-data-from-cgimage) talks about how to get the image as NSData, which you can store using Core Data. – Brendon Jan 24 '13 at 16:10

2 Answers2

0

Yes, you can convert the image to NSData and store that. Example:

Entity *testEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:__managedObjectContext];
NSString *photoPath = [[NSBundle mainBundle] pathForResource:@"photo" ofType:@"png"];
if ([[NSFileManager defaultManager] fileExistsAtPath:photoPath]) {
    NSData *data = [NSData dataWithContentsOfFile:photoPath];
    [testEntity setPhoto:data];
}

This stores the image as BLOB data in the sqlite file.

JDx
  • 2,615
  • 3
  • 22
  • 33
  • But that would be a compressed image already, wouldn't be? Once I would like to use it as a UIImage the jpg contained in that BLOB will have to pass through decompression process. Am I wrong? – crisisGriega Jan 24 '13 at 16:36
0

Ideally you never keep large number of UIImage objects of large images in memory.They will give u memory warnings. If the Images are local files, you can do one thing, using a background thread to scale the big images to the size which is ideal for the carousel.Save these thumb nails and map them to the original image. Load the thumb nails for carousel, and use the original image file for detailed image viewing.The thumb nails would be png for max performance.Jpeg decoding is not the native of iOS, and require more cpu to decode them than png.You dont have to keep the thumb nail data in core data, a .png file would do a nice job in my experience.You can use following code to load the image

 UIImage * image = [UIImage imageWithContentsOfFile:filePath];

Here is the code to resize image

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();

return newImage;

}

Tony Thomas
  • 967
  • 10
  • 20
  • I'm not using thumbnails. Basically it's just a scrollview. I'm loading the image in background "thread", but when I am scrolling too fast I can handle the previous threads so there it comes the memory warning – crisisGriega Jan 24 '13 at 16:38
  • You can put a placeholder image , then try to scale the large image at run time using blocks, this will make your memory requirement low, but the UI responsive – Tony Thomas Jan 24 '13 at 16:42
  • Thanks for the answer. I think I will upload some code (when I will have the chance) in order to be more descriptive about the issues. – crisisGriega Jan 24 '13 at 17:43