将UIImage数据保存到iOS中的照片库后取回它 [英] Get back UIImage data after saving it to photo Library in iOS

查看:113
本文介绍了将UIImage数据保存到iOS中的照片库后取回它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将我的应用程序创建的图像保存到iPhone库并尝试将其恢复后,我遇到了问题.图像数据有所不同.

I got an issue after saving an image created by my app into iPhone Library and trying to get it back. Image data are differents.

创建图片

1.Code

我不使用Alpha来创建图像(实际上,如果使用Alpha,我得到的结果相同)

I create an image without using alpha (In fact I got the same result if I use alpha)

+ (UIImage *)createImageFromData:(NSData *)data {
    uint8_t *imageData = (uint8_t *) calloc(WIDTH * HEIGHT * 3, sizeof(uint8_t));
    [data getBytes:imageData];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
                                                              imageData,
                                                              WIDTH * HEIGHT * 3,
                                                              NULL);
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGImageRef imageRef = CGImageCreate(WIDTH,
                                        HEIGHT,
                                        8, // Bits per color component
                                        3 * 8, // Bits per pixel : 1 pixel => 3 component
                                        3 * WIDTH, // bytes per lign (1 byte = 1 component)
                                        colorSpace,
                                        bitmapInfo,
                                        provider,
                                        NULL,
                                        NO,
                                        renderingIntent);
    UIImage *resultImage = [UIImage imageWithCGImage:imageRef];

    return resultImage;
}

2.Debug

当我在方法末尾插入中断时,这是lldb的结果

Here what I got with lldb when I put breaking at the end of the method

(lldb) p (int)imageData[0]
(uint8_t) $0 = 102
(lldb) p (int)imageData[1]
(uint8_t) $1 = 116
(lldb) p (int)imageData[2]
(uint8_t) $2 = 121
(lldb) p (int)imageData[3]
(uint8_t) $3 = 112

保存图像

- (IBAction)saveImage:(id)sender {
    UIImageWriteToSavedPhotosAlbum(_image, self,
   @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

我尝试了另一种保存图像的方法:

EDIT : I tried an alternative way to save the image :

CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.generatedImageView.image.CGImage));
uint8_t *data = (uint8_t *)CFDataGetBytePtr(pixelData);
NSData *dataToSave = [NSData dataWithBytesNoCopy:data length:CFDataGetLength(pixelData) freeWhenDone:NO];

ALAssetsLibrary *assetLib = [[ALAssetsLibrary alloc] init];
[assetLib writeImageDataToSavedPhotosAlbum:dataToSave metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
    NSLog(@"%@", error);
}];

DataToSave的字节数正确,在完成块中errorassetURL均为零,我在相册中看不到任何新图像

DataToSave got the right number of bytes, both error and assetURL are nil in the completion block, and I can't see any new image in the photos album

从库中获取图像

这是一个委托方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    // Handle a still image picked from a photo album
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
    == kCFCompareEqualTo) {
        self.image = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

从上一张图像中获取数据

1.Code

+ (NSData *)createDataFromImage:(UIImage *)image {
    CGImageRef imageRef = [image CGImage];

    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
    uint8_t *data = (uint8_t *)CFDataGetBytePtr(pixelData);

    return [NSData dataWithBytesNoCopy:data length:SIZE freeWhenDone:YES];;
}

2.Debug

现在我拥有的

(lldb) p (int)data[0]
(int) $4 = 35
(lldb) p (int)data[1]
(int) $5 = 72
(lldb) p (int)data[2]
(int) $6 = 100
(lldb) p (int)data[3]
(int) $7 = 255

出现的Alpha分量值为255 ...这应该不是问题吗?但是其他所有的价值都改变了.而且我真的不知道如何在这一点上获得先前的价值!

An alpha component appears with a value of 255 ... this should not be a problem ? But all others value have changed. And I really don't know how to get the previous value at this point !

这似乎是压缩(保存图像时)和解压缩(还原时)的问题. 但是在将其保存到相册"中后,我需要获取完全相同的数据.

EDIT : It seems to be an compression (when saving image) and decompression (when getting it back) problem. But I need to get the exact same data after saving it in the Photos album.

推荐答案

您的图像在保存到相册之前已被压缩为JPEG.并在加载时解压缩. JPEG是有损压缩.因此,当您比较像素数据时,原始图像与压缩/解压缩之间会有区别.

Your image is compressed to JPEG before it is saved to photo album. And is decompressed when you are loading it. JPEG is lossy compression. So when you are comparing pixel-data you have differences between original image and compressed/decompressed.

要保存无损,请尝试以下操作:

To save lossless try following:

+ (void)saveImageData:(NSData *)data {
    ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetLib writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:nil];
}

有关无损保存/从相册中加载/加载相册的更多信息,请阅读此主题

For more information about lossless saving/loading to/from photo album please read this thread Saving to/getting JPEG from user gallery without recompression

这篇关于将UIImage数据保存到iOS中的照片库后取回它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆