如何将RAW图像文件“上传”到iOS模拟器?

时间:2011-04-08 21:20:14

标签: cocoa-touch ios ios-simulator

如何将RAW图像文件“上传”到iOS模拟器,以便它在AssetsLibrary框架中的显示方式与通过相机连接套件从相机复制到iPad的原始图像相同?

(我知道如何在iOS模拟器中存储普通的JPEG和PNG图像。这不是问题。)

2 个答案:

答案 0 :(得分:1)

只需使用

writeImageDataToSavedPhotosAlbum:元数据:completionBlock:

将RAW文件的ImageData写入已保存的相册。 iOS支持多种RAW格式(肯定支持Canon和Nikon)。当您将RAW文件添加到库中时,AssetLibrary会自动为您的RAW文件创建jpeg和预览。

答案 1 :(得分:0)

我对RAW数据并不熟悉,但我能够将PNG图像转换为原始数据,然后转换回图像。这是我用来转换为图像的代码:

- (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer 
                                withWidth:(int) width
                               withHeight:(int) height {


    size_t bufferLength = width * height * 4;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
    size_t bitsPerComponent = 8;
    size_t bitsPerPixel = 32;
    size_t bytesPerRow = 4 * width;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    if(colorSpaceRef == NULL) {
        NSLog(@"Error allocating color space");
        CGDataProviderRelease(provider);
        return nil;
    }

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGImageRef iref = CGImageCreate(width, 
                                    height, 
                                    bitsPerComponent, 
                                    bitsPerPixel, 
                                    bytesPerRow, 
                                    colorSpaceRef, 
                                    bitmapInfo, 
                                    provider,   // data provider
                                    NULL,       // decode
                                    YES,            // should interpolate
                                    renderingIntent);

    uint32_t* pixels = (uint32_t*)malloc(bufferLength);

    if(pixels == NULL) {
        NSLog(@"Error: Memory not allocated for bitmap");
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpaceRef);
        CGImageRelease(iref);       
        return nil;
    }

    CGContextRef context = CGBitmapContextCreate(pixels, 
                                                 width, 
                                                 height, 
                                                 bitsPerComponent, 
                                                 bytesPerRow, 
                                                 colorSpaceRef, 
                                                 kCGImageAlphaPremultipliedLast); 

    if(context == NULL) {
        NSLog(@"Error context not created");
        free(pixels);
    }

    UIImage *image = nil;
    if(context) {

        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);

        CGImageRef imageRef = CGBitmapContextCreateImage(context);

        // Support both iPad 3.2 and iPhone 4 Retina displays with the correct scale
        if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
            float scale = [[UIScreen mainScreen] scale];
            image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
        } else {
            image = [UIImage imageWithCGImage:imageRef];
        }

        CGImageRelease(imageRef);   
        CGContextRelease(context);  
    }

    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(iref);
    CGDataProviderRelease(provider);

    if(pixels) {
        free(pixels);
    }   
    return image;
}

我真的不记得来源,但它对我有用。

您是否可以使用代码加载RAW图像?如果是,只需将数据传递给上述功能(您还需要正确转换图像的宽度和高度)。