我可以在Core Data中存储解压缩的图像

时间:2013-01-24 15:52:18

标签: ios core-data uiimage cgimageref

我正在制作一个包含大量大图像的转盘,我正在进行一些测试,试图提高加载图像的性能。现在,即使我已经在不同的队列中解压缩jpg,它仍然需要一点点,主要是与iOS中包含的相册应用程序进行比较。此外,如果我非常快速地传递图像,我可以产生记忆警告。

所以我要做的是将CGImageRef(或已经解压缩的UIImage:原始数据)存储到Core Data中。但是我发现的所有答案和选项都是使用UIImageJPegRepresentation,但这样做我会再次压缩图像,不是吗?

有人知道是否有办法?我是否错误地关注了这个问题?

2 个答案:

答案 0 :(得分:0)

是的,您可以将图像转换为NSData并存储它。例如:

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];
}

将图像作为BLOB数据存储在sqlite文件中。

答案 1 :(得分:0)

理想情况下,你永远不会在内存中保留大量大图像的UIImage对象。它们会给你内存警告。 如果图像是本地文件,您可以做一件事,使用背景线程将大图像缩放到适合旋转木马的大小。保存这些拇指指甲并将它们映射到原始图像。 加载拇指指甲用于旋转木马,并使用原始图像文件进行详细的图像查看。拇指指甲将是png以获得最佳性能.Jpeg解码不是iOS的原生,并且需要更多的cpu来解码它们而不是png.You没有为了将拇指指甲数据保存在核心数据中,.png文件在我的经验中会做得很好。您可以使用以下代码加载图像

 UIImage * image = [UIImage imageWithContentsOfFile:filePath];

以下是调整图片大小的代码

- (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;

}