首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将原始图像文件“上传”到iOS模拟器?

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

Stack Overflow用户
提问于 2011-04-09 05:20:14
回答 2查看 1.1K关注 0票数 2

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

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

EN

回答 2

Stack Overflow用户

发布于 2011-05-05 18:20:54

只需使用

writeImageDataToSavedPhotosAlbum:metadata:completionBlock:

若要将原始文件的ImageData写入已保存的相册,请执行以下操作。iOS支持许多RAW格式(绝对支持佳能和尼康)。当您将原始文件添加到库中时,AssetLibrary会自动为原始文件创建jpeg和预览。

票数 1
EN

Stack Overflow用户

发布于 2011-04-09 19:03:32

我不太熟悉原始数据,但我能够将PNG图像转换为原始数据,然后再转换回图像。下面是我用来转换为图片的代码:

代码语言:javascript
复制
- (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;
}

我真的记不住来源了,但它对我很有效。

你能用你的代码加载原始图像吗?如果是,只需将数据传递给上面的函数(您还需要图像的宽度和高度才能正确转换)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5600898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档