IPhone, how to read image from photo library like NSData?

On iPhone, I saved the image in the photo library with the changes (adding data after the image code) using the assest library to save the NSData directly in the photo library. Now I want to read the image from the photo library as NSData in order to read it. If I read it as a UIImage, it will change the data inside the image.

How can I read a photo from a photo library like NSData? I tried to look at the link url in 4.1 but no luck.

Edit: I edited to explain why I save it as NSData. The URL method in the answers works if you just need pure NSData but it doesn’t help in the context that I am asking for.

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

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

This allowed me to get everything inside the file that gave me the PLUS image code, which I added in the form of NSData.

Edit 3:

Is there a way to do this now in iOS 10 using PHPhotoLibrary, which replaces AssetLibrary? I need the image in NSData to be the original RAW image without any changes.

+8
iphone xcode uiimage uiimagepickercontroller nsdata
source share
2 answers

You should learn the getBytes method for ALAssetRepresentation. This gives you the raw RAW image data of your image, and also allows you to use a buffer to process the image instead of reading the image into memory at the same time. And, of course, you can always generate NSData from the getBytes method.

Greetings

Hendrick

+3
source share

If you already have ALAsset , you can get the default NSData views using the following:

 NSData *data = [NSData dataWithContentsOfURL:[[asset defaultRepresentation] url]]; 
+1
source share

All Articles