iPad上最快的图像格式是什么?

时间:2012-09-17 15:07:05

标签: ios uiimage cgimage

假设以下内容:

  • 您在ipad应用中从互联网下载图像
  • 您可以按照您想要的任何方式对图像进行后处理,所花费的时间并不重要(下载数据时为1次操作)。设备上的实际表示也无关紧要。
  • 你可以任意方式编写该图像的加载代码,只要它能产生UIImage

问题是:在iPad上存储图像的最佳格式是什么,因此加载它需要花费最少的时间?某种原始转储的CG ...上下文位图内存?

1 个答案:

答案 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;
}