Frage

Suppose the following:

  • you download an image from the internet in your ipad app
  • you can postprocess the image any way you want and the time it takes is not important (1 time operation when downloading data). The actual representation on the device does not matter either.
  • you can write the loading code for that image any way you want, as long as it results in a UIImage

The question is: what is the best format to store an image on the iPad so loading it takes the least time possible? Some kind of raw dump of the CG...Context bitmap memory?

War es hilfreich?

Lösung

In the mean time I think I have figured it out:

to save the UIImage I added this method in a category:

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

To load it I added this:

+(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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top