Note that there are some explanatory texts on larger screens.

plurals
  1. POZeroing ALAssetRepresentation after saving image captured via UIImagePickerController in iOS5
    primarykey
    data
    text
    <p>In my app (which worked under iOS 4) I collect pictures selected via <strong>UIImagePickerController</strong>. Unfortunately, I have a strange problem after upgrading to iOS 5.</p> <p>In a nutshell, I store <strong>ALAssetRepresentation</strong> in <strong>NSMutableArray</strong>. When I add photos from Library, everything is ok. However, when I capture and save a picture, all <strong>ALAssetRepresentations</strong> (including a new one) become 0-sized. <strong>ALAssetRepresentation.size</strong> and <strong>ALAssetRepresentation.getBytes:fromOffset:length:error:</strong> return 0 and <strong>getBytes:error</strong> is nil.</p> <p>I init <strong>ALAssetsLibrary</strong> in <strong>AppDelegate</strong>, so the <em>“The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.”</em> condition is OK.</p> <p>Is there a way to prevent ALAssetRepresentation from zeroing? Or how can I read image by bytes after this?</p> <p>My code:</p> <pre><code>-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary){ [self addPhoto:[info valueForKey:UIImagePickerControllerReferenceURL]]; } else if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera){ [self savePhoto:[info valueForKey:UIImagePickerControllerOriginalImage]]; } [self dismissModalViewControllerAnimated:YES]; } -(ALAssetsLibrary*) getLibrary{ if (!library){ testAppDelegate *appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate]; library = appDelegate.library; } NSLog(@"getLibrary: %@", library); return library; } -(void) addPhoto:(NSURL*) url{ ALAssetsLibraryAssetForURLResultBlock successBlock = ^(ALAsset *asset_){ ALAssetRepresentation *assetRepresentation = [[asset_ defaultRepresentation] retain]; [photos addObject: assetRepresentation]; }; ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){ NSLog(@"Error: Cannot get image. %@", [error localizedDescription]); }; [[self getLibrary] assetForURL:url resultBlock:successBlock failureBlock:failureBlock]; } - (void)savePhoto:(UIImage *)image { [[self getLibrary] writeImageToSavedPhotosAlbum:[image CGImage] orientation:[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error) { if (error) { NSLog(@"Error: Cannot save image. %@", [error localizedDescription]); } else { NSLog(@"photo saved"); [self addPhoto:assetURL]; } }]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload