Download UIImage from UIImagePickerControllerReferenceURL

I use UIImagePickerController to allow users to select an image from an image library. Then I want to start placing this file in the sqlite database so that I can access it later.

I search the Internet for how to do this, and I come quite shortly. I know that I can get the ReferenceURL of an element by calling the delegate method:

NSURL *picURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"] 

This gives me a valid NSURL object, and if I print picURL.absoluteString, I can indicate that it is a valid url for the location of the library file. However, if I try to create an NSData object with this URL so that I can in turn create a UIImage, the code will not be executed because NSData returns nil. For instance:

  NSURL *picURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"] NSString *stringUrl = picURL.absoluteString; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringUrl]]; imgMain.image = [UIImage imageWithData:data] 

I understand that I am repeating myself a bit here, but I need to take a string representation of the URL that I can store in sqlite, and then use the specified string later to create the image (so that the code above is a sequence of thoughts).

I found some info popping up about using ALAssetsLibrary, but it gave me all kinds of tricks, since it doesn't seem to play well with iOS 5 and ARC. I keep getting error messages. The ALAsset receiver type for the instance message is a forward declaration. I can set properties in a file that references ALAsset / ALAssetsLibrary to ignore ARC, which stops the build error, but then as soon as I leave the code block, the data goes away. See below:

  NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; __block UIImage *returnValue = nil; [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) { returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]; } failureBlock:^(NSError *error) { // error handling }]; [library release]; 

returnValue returns the correct image, but as soon as the code exits the result. The data content lock disappears (i.e. returnValue becomes zero again).

So my question is: how can I use the UIImagePickerControllerReferenceURL and convert it to a string that I can store in sqlite and then use the specified string to create a valid UIImage object in the future?

+6
source share
1 answer

It looks like you are on the right track to save the resource URL.

 Save NSURL *picURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"] NSString *stringUrl = picURL.absoluteString; Reload AssetURL NSURL asssetURL = [NSURL URLWithString:stringUrl] 

As for loading the asset later, I'm going to rely on the assumption that you are not using an arc. Which would explain why you returnValue is null.

Did you try to add a save to the result? eg:

  NSURL *referenceURL = [NSURL URLWithString:savedAssetURLString] ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; __block UIImage *returnValue = nil; [library assetForURL:referenceURL resultBlock:^(ALAsset *asset) { returnValue = [[UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]] retain]; //Retain Added } failureBlock:^(NSError *error) { // error handling }]; [library release]; 
+9
source

Source: https://habr.com/ru/post/917840/


All Articles