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) {
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?