假设以下内容:
问题是:在iPad上存储图像的最佳格式是什么,因此加载它需要花费最少的时间?某种原始转储的CG ...上下文位图内存?
答案 0 :(得分:0)
与此同时,我认为我已经弄明白了:
保存UIImage我在一个类别中添加了这个方法:
typedef struct
{
int width;
int height;
int scale;
int bitsPerComponent;
int bitsPerPixel;
int bytesPerRow;
CGBitmapInfo bitmapInfo;
} ImageInfo;
-(void)saveOptimizedRepresentation:(NSString *)outputPath
{
NSData * pixelData = (__bridge_transfer NSData *)CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage));
CGSize size;
size.width = CGImageGetWidth(self.CGImage);
size.height = CGImageGetHeight(self.CGImage);
int bitsPerComponent = CGImageGetBitsPerComponent(self.CGImage);
int bitsPerPixel = CGImageGetBitsPerPixel(self.CGImage);
int bytesPerRow = CGImageGetBytesPerRow(self.CGImage);
int scale = self.scale;
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(self.CGImage);
ImageInfo info;
info.width = size.width;
info.height = size.height;
info.bitsPerComponent = bitsPerComponent;
info.bitsPerPixel = bitsPerPixel;
info.bytesPerRow = bytesPerRow;
info.bitmapInfo = bitmapInfo;
info.scale = scale;
//kCGColorSpaceGenericRGB
NSMutableData * fileData = [NSMutableData new];
[fileData appendBytes:&info length:sizeof(info)];
[fileData appendData:pixelData];
[fileData writeToFile:outputPath atomically:YES];
}
要加载它,我添加了这个:
+(UIImage *)loadOptimizedRepresentation:(NSString *)inputPath
{
FILE * f = fopen([inputPath cStringUsingEncoding:NSASCIIStringEncoding],"rb");
if (!f) return nil;
fseek(f, 0, SEEK_END);
int length = ftell(f) - sizeof(ImageInfo);
fseek(f, 0, SEEK_SET);
ImageInfo info;
fread(&info, 1, sizeof(ImageInfo), f);
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
info.width,
info.height,
info.bitsPerComponent,
info.bytesPerRow,
cs,
info.bitmapInfo
);
void * targetData = CGBitmapContextGetData(bitmapContext);
fread(targetData,1,length,f);
fclose(f);
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(bitmapContext);
UIImage * result = [UIImage imageWithCGImage:decompressedImageRef scale:info.scale orientation:UIImageOrientationUp];
CGContextRelease(bitmapContext);
CGImageRelease(decompressedImageRef);
CGColorSpaceRelease(cs);
return result;
}