How to create a new ALAsset from an uploaded image

I am trying to develop an application that will download images (from the photo site) and create an ALAsset for each image, and then place them under the new ALAssetsGroup.

I can create a new album (ALAssetsGroup) and download data from the website. However, I am a little fixated on how to create a new ALAsset.

I tried to do the following

ALAsset *asset = [[[ALAsset alloc] init] autorelease]; NSDictionary *metadata = [NSDictionary dictionaryWithObjectsAndKeys:p.id, @"id", p.thumbnail_url, @"thumbnail_url", p.photo_url, @"photo_url", nil]; [asset setImageData:data metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) { ESLog(@"Asset %@ created error:%@", assetURL, error); [group addAsset:asset]; }]; 

However, I get fingerprints in which both propertyURL and the error are empty.

 2012-04-15 02:58:06.850 XXXXXX.com[5966:c607] Asset (null) created error:(null) 

It would be great if anyone can suggest how I can create a new Asset in the album.

+7
source share
3 answers

You cannot create a new ALAsset. What you need to do is save the image data in the Photo Library using the method:

 - (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock 

The completion block will return NSURL for the newly created asset. Using the method

 - (void)assetForURL:(NSURL *)assetURL resultBlock:(ALAssetsLibraryAssetForURLResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock 

with NSURL, it will return to you an ALAsset instance of the newly created asset.

Greetings.

Hendrick

+6
source

You can use the UIImageWriteToSavedPhotosAlbum function to save the UIImage in the camera poll.

 void UIImageWriteToSavedPhotosAlbum ( UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo ); 

It is described here.

Then you can select it from the Asset library.

+1
source

Even easier to use the following code:

  //Save to photo album UIImage *img = [[[UIImage alloc] initWithData:imageData] autorelease]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library writeImageToSavedPhotosAlbum:img.CGImage metadata:[info objectForKey:UIImagePickerControllerMediaMetadata] completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"assetURL %@", assetURL); }]; 
+1
source

All Articles