2012-09-17 24 views
1

假設如下:iPad上最快的圖像格式是什麼?

  • 您從互聯網在你的iPad應用程序下載的圖像
  • 可以後期處理圖像任何你想要的方式和所花費的時間並不重要(下載時,1個運行數據)。設備上的實際表示也無關緊要。
  • 你可以寫該圖像的加載代碼,你想要的任何方式,只要它導致一個UIImage

的問題是:什麼是存儲圖像在iPad上所以加載它的最佳格式花費最少的時間?某種類型的CG原始轉儲...上下文位圖內存?

回答

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; 
} 
+0

因此您將圖像存儲在文件系統的文件中。你可以詳細說明節省成本,而不是僅僅在文件系統中存儲JPG或PNG。甚至比較保存在數據庫中? – Bjinse

相關問題