【问题标题】:How can I "upload" a RAW image file to the iOS simulator?如何将 RAW 图像文件“上传”到 iOS 模拟器?
【发布时间】:2011-08-01 20:03:08
【问题描述】:

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

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

【问题讨论】:

    标签: cocoa-touch ios ios-simulator


    【解决方案1】:

    随便用

    writeImageDataToSavedPhotosAlbum:metadata:completionBlock:

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

    【讨论】:

      【解决方案2】:

      我不太熟悉 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 图像?如果是,只需将数据传递给上述函数(您还需要正确转换图像的宽度和高度)。

      【讨论】:

      • 抱歉,您误解了“原始图像”在此上下文中的含义。这里的“RAW”是指相机特定的原始文件格式类别,如 .cr2(佳能)和 .nef(尼康),其中包含直接来自相机传感器的每像素数据(参见 Wikipedia 中的“滤色器阵列”),外加曝光元数据等,编码在非公开文档中(但在大多数情况下,反向工程和广为人知,谷歌为“dcraw”)。一些这样的格式甚至是部分加密的。您会想到“原始”解压缩的每通道 8 位 RGB 数据,这是完全不同的事情。
      猜你喜欢
      • 2021-10-23
      • 2012-11-06
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多