Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS5 Saving images to custom folder, ALAssetsLibrary fail

<><> ---- Aalok has answered this question, I'm just waiting for him to write it up so I can chose that as the correct answer. Until then, along with making the changes he describes below I had to call -- self.library = [[ALAssetsLibrary alloc] init]; -- before every attempt to save my image. As a precaution I also stopped the AVSession running until after the save, when is was restarted (using [session stopRunning]; and [session startRunning]; ----- <><>

<><> ----- EDIT 2: You do not need to stop and restart the AV session, tested this thoroughly and it's working perfectly. ----- <><>

<><> ----- EDIT 3: after testing this on my device thoroughly, and it working perfectly, the code is not working once it's been through the review process and put on the store. Two identical devices (2x iPhone 4) running the same OS, one using my dev build, one off the app sotre, the ap store version still has the bug. Giving up with this for now ----- <><>

I'm using the category in this link to try to save to a custom folder:

http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/

Now it works some of the time, but not all. In the comments it's been suggested that the following code will detect if the group properties are nil:

    if ([group valueForProperty:ALAssetsGroupPropertyURL] == nil)
{
NSLog(@”group properties are nil!”);
} else {
[group addAsset:asset];
}

Which I have, and it does detect if the properties are nil. So all good. What I'm struggling with is at that point setting the properties and saving the images. I'm guessing that at this point we can manually set the albumName and save the image, which I've tried, but the error still occurs.

Any ideas? Thank you.

like image 792
mrEmpty Avatar asked Jun 26 '12 13:06

mrEmpty


1 Answers

I have facing the same problem with this same .h and .m file but find one solution for this after working on it for 2 to 3 days and the solution is very simple what I have to change is in .m file in

-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock

and in

-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock

methods i just add this before calling the inside methods

//THE CHANGE dispatch_async(dispatch_get_main_queue(),^{ //
[self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL* assetURL, NSError* error) 
    {
        //error handling
        if (error!=nil) 
        {
            completionBlock(error);
            return;
        }
        //add the asset to the custom photo album
        [self addAssetURL: assetURL toAlbum:albumName withCompletionBlock:completionBlock];
    }];
//THE CHANGE }); //

AND Same for the other one

Happy Coding :)

EDIT

In second method I add a line below every completionBlock(nil); line

[[NSNotificationCenter defaultCenter] postNotificationName:kSaveSuccess object:nil];

And I use this notification to make sure that image is saved in album too. Till that time I Shows UIActivityIndicator with some text message and after successfully saved image one popup message is shown indicated that image is saved in album with album name. And while this time the UI is unresponsive i.e. user can't do any thing other then pressing home button of device :) ;)

Happy Coding :)

like image 191
The iOSDev Avatar answered Sep 21 '22 21:09

The iOSDev