【发布时间】:2011-08-01 20:03:08
【问题描述】:
如何将 RAW 图像文件“上传”到 iOS 模拟器,使其在 AssetsLibrary 框架中的显示方式与通过相机连接工具包从相机复制到 iPad 的原始图像相同?
(我确实知道如何在 iOS 模拟器中存储普通的 JPEG 和 PNG 图像。这不是问题。)
【问题讨论】:
标签: cocoa-touch ios ios-simulator
如何将 RAW 图像文件“上传”到 iOS 模拟器,使其在 AssetsLibrary 框架中的显示方式与通过相机连接工具包从相机复制到 iPad 的原始图像相同?
(我确实知道如何在 iOS 模拟器中存储普通的 JPEG 和 PNG 图像。这不是问题。)
【问题讨论】:
标签: cocoa-touch ios ios-simulator
随便用
writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
将 RAW 文件的 ImageData 写入已保存的相册。 iOS 支持多种 RAW 格式(肯定支持佳能和尼康)。当您将 RAW 文件添加到资源库时,AssetLibrary 会自动为您的 RAW 文件创建 jpeg 和预览。
【讨论】:
我不太熟悉 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 图像?如果是,只需将数据传递给上述函数(您还需要正确转换图像的宽度和高度)。
【讨论】: