iPhone,如何从照片库中读取图像作为NSData?

时间:2022-11-14 13:08:33

On the iPhone I saved an image to the photo library with modifications (appending data after the image code) using the assest library to save the NSData directly to the photo library. Now I want to read the image back from the photo library as an NSData to read it. If I read it as a UIImage, it changes the data inside the image.

在iPhone上,我使用assest库将图像保存(即在图像代码后附加数据),将图像保存到照片库,将NSData直接保存到照片库中。现在我想从照片库中读回图像作为NSData来读取它。如果我将其作为UIImage读取,它会更改图像内的数据。

How can I read the photo from the photo library as NSData? I tried looking into the reference URL in 4.1 but no luck.

如何将照片库中的照片作为NSData读取?我尝试在4.1中查看参考URL,但没有运气。

Edit: I edited to explain why I'm saving it as an NSData. The URL method in the answers works if you just want pure NSData, but does not help in the context that I am asking.

编辑:我编辑解释为什么我将其保存为NSData。如果您只想要纯NSData,则答案中的URL方法有效,但在我要求的上下文中没有帮助。

Edit 2: The answer with the getBytes was the answer that did it for me. The code I used was:

编辑2:getBytes的答案是为我做的答案。我使用的代码是:

int8_t *bytes = malloc([representation size]);
NSUInteger length = [representation getBytes:bytes fromOffset:0 length:[representation size] error:&error];

This was able to get me everything inside the file which gave me the image code PLUS what I added in NSData form.

这能够让我得到文件里面的所有内容,它给了我在NSData表单中添加的图像代码PLUS。

Edit 3:

Is there a way to do this now in iOS 10 with the PHPhotoLibrary which replaces AssetLibrary? I need the image in NSData being the original RAW image with no modifications.

现在有没有办法在iOS 10中使用PHPhotoLibrary替换AssetLibrary?我需要NSData中的图像是原始的RAW图像,没有任何修改。

2 个解决方案

#1


3  

You should look into the getBytes method of ALAssetRepresentation. This gives you the original RAW image data of your image and also makes it possible to use a buffer to process the image instead of reading the image into memory all at once. And of course you can always generate NSData from the getBytes method.

您应该查看ALAssetRepresentation的getBytes方法。这样可以为您提供图像的原始RAW图像数据,还可以使用缓冲区处理图像,而不是一次性将图像读入内存。当然,您始终可以从getBytes方法生成NSData。

Cheers,

Hendrik

#2


1  

If you already have your ALAsset, you should be able to get an NSData of the default representation using the following:

如果您已经拥有ALAsset,则应该能够使用以下内容获取默认表示的NSData:

NSData *data = [NSData dataWithContentsOfURL:[[asset defaultRepresentation] url]];

#1


3  

You should look into the getBytes method of ALAssetRepresentation. This gives you the original RAW image data of your image and also makes it possible to use a buffer to process the image instead of reading the image into memory all at once. And of course you can always generate NSData from the getBytes method.

您应该查看ALAssetRepresentation的getBytes方法。这样可以为您提供图像的原始RAW图像数据,还可以使用缓冲区处理图像,而不是一次性将图像读入内存。当然,您始终可以从getBytes方法生成NSData。

Cheers,

Hendrik

#2


1  

If you already have your ALAsset, you should be able to get an NSData of the default representation using the following:

如果您已经拥有ALAsset,则应该能够使用以下内容获取默认表示的NSData:

NSData *data = [NSData dataWithContentsOfURL:[[asset defaultRepresentation] url]];