Zeroing ALAssetRepresentation after saving image captured via UIImagePickerController in iOS5 - ios5

In my app (which worked under iOS 4) I collect pictures selected via UIImagePickerController. Unfortunately, I have a strange problem after upgrading to iOS 5.
In a nutshell, I store ALAssetRepresentation in NSMutableArray. When I add photos from Library, everything is ok. However, when I capture and save a picture, all ALAssetRepresentations (including a new one) become 0-sized. ALAssetRepresentation.size and ALAssetRepresentation.getBytes:fromOffset:length:error: return 0 and getBytes:error is nil.
I init ALAssetsLibrary in AppDelegate, so the
“The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.” condition is OK.
Is there a way to prevent ALAssetRepresentation from zeroing? Or how can I read image by bytes after this?
My 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];
}
}];
}

I solved it!
ALAssetsLibraryChangedNotification
Sent when the contents of the assets library have changed from under the app that is using the data.
When you receive this notification, you should discard any cached information and query the assets library again. You should consider invalid any ALAsset, ALAssetsGroup, or ALAssetRepresentation objects you are referencing after finishing processing the notification.

Have you tried retaining the ALAssets instead of the ALAssetRepresentation? This should work, I have used this approach before.

Related

UIImagePickerControllerReferenceURL is missing

I'm trying to get some data about a video i chose in the UIImagePicker.
So when it gets into the UIImagePicker delegate method (below) i understand i need to use the UIImagePickerControllerReferenceURL key from the info dictionary.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
NSLog(#"%#",url);
[library addAssetURL:[info objectForKey:UIImagePickerControllerReferenceURL] toAlbum:#"Compedia" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
}
The problem is that the url is resulting with nil.
I printed the description of info and it looks like this:
Printing description of info:
{
UIImagePickerControllerMediaType = "public.movie";
UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/6630FBD3-1212-4ED0-BC3B-0C23AEEFB267/tmp/capture-T0x1f57e880.tmp.Ulfn5o/capturedvideo.MOV";
}
After i've done some research i found out that if i set the camera with kUTTypeMovie it should give me both media and reference url.
This is how i defined my camera:
cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
cameraUI.allowsEditing = YES;
cameraUI.delegate = delegate;
cameraUI.showsCameraControls = NO;
cameraUI.cameraOverlayView = [self getCustomToolBar];
Is there something i'm missing?
Thanks,
Nearly went insane on this one. Every where I looked everyone seemed to be convinced that the UIImagePickerControllerReferenceURL was always there.
Anyway the issue is that if your delegate is being called after you have taken a pic using the camera than the UIImagePickerControllerReferenceURL object will not be there as the image has not been saved to the camera roll yet so therefore there is no UIImagePickerControllerReferenceURL. It is only there when the delegate is called after selecting an image from the camera roll.
So my way around this is to use the ALAssetLibrary to save it to the camera roll then read back the URL.
Heres my solution:
First you want to detect your PickerController sourceType to see if it was the camera or photosLibrary/savedPhotosAlbum. If it is the camera then we use the Asset library's writeImageToSavedPhotosAlbum:orientation:completionBlock: to write the image to the camera roll and give us back the images URL in the completion block.
However if the its not the camera then that means is was either the photosLibrary or savedPhotosAlbum which in both cases [info objectForKey:UIImagePickerControllerReferenceURL] would return a valid url
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//dismiss the imagepicker view
[self dismissViewControllerAnimated:YES completion:nil];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera )
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibrary *library = [Utils defaultAssetsLibrary];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
{
//here is your URL : assetURL
}];
}
else
{
//else this is valid : [info objectForKey:UIImagePickerControllerReferenceURL]];
}
}
Hope that helps.
ALAssetsLibrary is deprecated... use PHFetchOptions..
Refer below answer:
https://stackoverflow.com/a/44328085/5315917

Save UIImage to Photo Album with writeImageToSavedPhotosAlbum

I'm trying to save a UIImage to Photo Album. I've tried severl methods the last one is:
-(IBAction)captureLocalImage:(id)sender{
[photoCaptureButton setEnabled:NO];
// Save to assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum: imageView.image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error2)
{
// report_memory(#"After writing to library");
if (error2) {
NSLog(#"ERROR: the image failed to be written");
}
else {
NSLog(#"PHOTO SAVED - assetURL: %#", assetURL);
}
runOnMainQueueWithoutDeadlocking(^{
// report_memory(#"Operation completed");
[photoCaptureButton setEnabled:YES];
});
}];
}
imageView is a UIImageView which contain the image I want to save.
On log I got "PHOTO SAVED - assetURL: (null)" and the photo doesn't save to library.
What am I doing wrong?
just use this bellow line for save the image in your photo library
UIImageWriteToSavedPhotosAlbum(imageView.image,nil,nil,nil);
:)
For iOS 11+ needs Privacy - Photo Library Additions Usage Description in info.plist
Add NSPhotoLibraryAddUsageDescription in info.plist
Code:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Reference
Check out this sample.. which explains how to save photos using assets library..
you can even use..
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
cehck out his link How to save picture to iPhone photo library?
I'm using GPUImage, Apparently there is some kind of problem with the library itself.
The code above is absolutely working. If you want to read more about this issue:
GPUImagePicture with imageFromCurrentlyProcessedOutput doesn't get UIImage
Since iOS 8.1 UIKit framework has the UIImageWriteToSavedPhotosAlbum() function as vishy said.
I use it in this way:
UIImageWriteToSavedPhotosAlbum(myImage, self, "image:didFinishSavingWithError:contextInfo:", nil)
and then
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
// check error, report image has been saved, ...
}
When you take a photo from camera, then you can save image in picker delegate:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
[library writeImageToSavedPhotosAlbum:image.CGImage metadata:[info objectForKey:UIImagePickerControllerMediaMetadata] completionBlock:^(NSURL *assetURL, NSError *error) {
...
}];
[picker dismissViewControllerAnimated:YES completion:nil];
}

ALAssetsLibrary - crash after receiving ALAssetsLibraryChangedNotification

Part of my app has a photo browser, somewhat similar to Apple's Photos app, with an initial view controller to browse photo thumbnails and a detail view that's shown when you tap on a photo.
I'm using ALAssetsLibrary to access photos, and I pass an array of ALAsset URL's to my detail view controller so you can swipe from one photo to the next.
Everything works great, until I receive an ALAssetsLibraryChangedNotification while swiping from one photo to another (in the detail view controller), which often results in a crash:
NOTIFICATION: the asset library changed // my own NSLog for when the
notification occurs
loading assets... // my own NSLog for when I start reloading assets in
the thumbnail browser
Assertion failed: (size == bytesRead), function
-[ALAssetRepresentation _imageData], file /SourceCache/AssetsLibrary/MobileSlideShow-1373.58.1/Sources/ALAssetRepresentation.m,
line 224.
The specific line of code it crashes on, is in calling [currentRep metadata] as shown here:
- (void)someMethod {
NSURL *assetURL = [self.assetURLsArray objectAtIndex:index];
ALAsset *currentAsset;
[self.assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[self performSelectorInBackground:#selector(configureDetailViewForAsset:) withObject:asset];
} failureBlock:^(NSError *error) {
NSLog(#"failed to retrieve asset: %#", error);
}];
}
- (void)configureDetailViewForAsset:(ALAsset *)currentAsset {
ALAssetRepresentation *currentRep = [currentAsset defaultRepresentation];
if (currentAsset != nil) {
// do some stuff
}
else {
NSLog(#"ERROR: currentAsset is nil");
}
NSDictionary *metaDictionary;
if (currentRep != nil) {
metaDictionary = [currentRep metadata];
// do some other stuff
}
else {
NSLog(#"ERROR: currentRep is nil");
}
}
I understand that once a notification is received, it invalidates any references to ALAsset and ALAssetRepresentation objects... but how am I supposed to deal with the situation where it invalidates something right in the middle of trying to access it?
I've tried setting a BOOL, right when receiving the notification to completely abort and prevent [currentRep metadata] from ever being called, but even that doesn't catch it every time:
if (self.receivedLibraryChangeNotification) {
NSLog(#"received library change notification, need to abort");
}
else {
metaDictionary = [currentRep metadata];
}
Is there anything I can do? At this point I'm almost ready to give up on using the ALAssetsLibrary framework.
(note this unresolved thread on the Apple dev forums describing the same issue: https://devforums.apple.com/message/604430 )
It seems the problem is around here:
[self.assetsLibrary assetForURL:nextURL
resultBlock:^(ALAsset *asset) {
// You should do some stuff with asset at this scope
ALAssetRepresentation *currentRep = [asset defaultRepresentation];
// Assume we have a property for that
self.assetRepresentationMetadata = [currentRep metadata];
...
// assume we have a method for that
[self updateAssetDetailsView];
}
failureBlock:^(NSError *error) {
NSLog(#"failed to retrieve asset: %#", error);
}];
Once you have got user asset it is better to copy asset information by providing necessary data to your details controller subviews or by caching for later use. It can be helpful for avoiding ALAsset invalidation troubles. When notification ALAssetsLibraryChangedNotification sent you may need to discard details controller and query the Library content from the beginning.

How to call UIImagePickerController Delegate programmatically or forcefully [without user interaction]?

In my app, i have Used UiimagepickerController for taking Video.in bettween my programm received any web service Which belongs to my app,
i have to stop Video Capture and save video.
i have Used StopVideoCapture to do above thing ,but it doesn't call delegate - `
(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
How to force call above delegate ??.or How to handle interruption Handling inUIImagePickerController`.. any idea?
The idea with delegate methods is not that you call those methods - "They call you".
So I would not consider calling the delegate method yourself a good practise. However, if you present the UIImagePickerViewController with a modal dialogue (which I guess is common for such a picker) then you can close it like this outside of your delegate method:
[[yourPicker parentViewController] dismissModalViewControllerAnimated:YES];
Source
Update: You can use the ALAssetsLibrary for accessing the stored data in your iPhone media library. I recently had to do a similar project where I had to list all images on the iPhone. The Github project ELCImagePickerController.git was very useful since it shows how the items in your library can be accessed. So you'll do something like this:
#import <AssetsLibrary/AssetsLibrary.h>
// ....
-(void)fetchPhotoAlbums{
if(!self.assetsGroups){
self.assetsGroups = [NSMutableDictionary dictionary];
}
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
#autoreleasepool {
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop){
if (group == nil){
// Completed
[self.delegate pictureService:self fetchedAlbums:returnArray];
return;
}
Album *currentAlbum = [self albumForAssetsGroup:group];
// Store the Group for later retrieving the pictures for the album
[self.assetsGroups setObject:group forKey:currentAlbum.identifier];
[returnArray addObject:currentAlbum];
[self.delegate pictureService:self fetchedAlbums:returnArray];
};
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
NSLog(#"A problem occured %#", [error description]);
};
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];
}
}
-(void)fetchPhotosForAlbum:(Album *)album{
ALAssetsGroup *currentGroup = [self.assetsGroups objectForKey:album.identifier];
NSMutableArray *photos = [NSMutableArray array];
[currentGroup enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if(asset == nil){
[self.delegate pictureService:self fetchedPictures:photos forAlbum:album];
return;
}
[photos addObject:[self pictureForAsset:asset]];
}];
}
Additionally I use two mapper methods to convert the AL-classes into my own model classes.
- (Album *)albumForAssetsGroup:(ALAssetsGroup *)assetsGroup{
Album *album = [[Album alloc] init];
album.title = [assetsGroup valueForProperty:ALAssetsGroupPropertyName];
album.identifier = [assetsGroup valueForProperty: ALAssetsGroupPropertyPersistentID];
album.assetsCount = assetsGroup.numberOfAssets;
album.thumbnail = [UIImage imageWithCGImage:assetsGroup.posterImage];
return album;
}
- (Picture *)pictureForAsset:(ALAsset *)asset{
Picture *picture = [[Picture alloc]init];
picture.identifier = [((NSArray *)[asset valueForProperty: ALAssetPropertyRepresentations]) objectAtIndex:0];
picture.thumbnail = [UIImage imageWithCGImage:asset.thumbnail];
return picture;
}
See the AssetsLibrary Documentation

assetForURL with ios 5.0 doesn't work with device

- (void)thumbnail:(NSNumber *)index{
__block NSNumber *number = [NSNumber numberWithInt:[index intValue]];
ALAssetsLibrary *library = [ALAssetsLibrary sharedALAssetsLibrary];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
CGImageRef iref = [myasset thumbnail];
if (iref) {
[delegate thumbnailDidLoad:[UIImage imageWithCGImage:iref] withIndex:number];
}
NSLog(#"RESSSSSSSSSSSSSSSSSSSSSSSSSSSSSULT");
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(#"Error, can't get image - %#",[myerror localizedDescription]);
};
NSString *mediaurl = #"assets-library://asset/asset.JPG?id=5AF4118C-947D-4097-910E-47E19553039C&ext=JPG";
NSURL *asseturl = [NSURL URLWithString:mediaurl];
[library assetForURL:asseturl resultBlock:resultblock failureBlock:failureblock];
NSLog(#"asseturl %#",asseturl);
}
Here is my code and i have issue with my blocks - they works under simulator 5.0 but they don't work under device at all, it doesn't stop on break points and NSLogs don't work. With simulator all work correctly. Notice: CLAuthorizationStatus == kCLAuthorizationStatusAuthorized
Make sure that this whole function - (void)thumbnail:(NSNumber *)index... is either executed from the main thread or you are sure that the user has authorized your app to use location services. If you call it in the background and you don't yet have authorization, then the user will never be prompted for approval and neither the result nor failure blocks will be called.
as of iOS5 assetForURL works async. Make sure you call
[delegate thumbnailDidLoad:[UIImage imageWithCGImage:iref] withIndex:number];
on the main thread. This is easiest accomplished by using dispatch_async on the main queue.
Cheers,
Hendrik