Kjuly / ALAssetsLibrary-CustomPhotoAlbum

A nice ALAssetsLibrary category for saving images & videos into custom photo album.
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
MIT License
405 stars 101 forks source link

addAssetURLToAlbum no longer public #9

Closed sigmundfridge closed 11 years ago

sigmundfridge commented 11 years ago

Hi, Using the code previously hosted on touchcode it was possible to move an existing image to the custom album. With the github code it seems the public methods only allow saving to the custom album such that if the imagepicker selects an existing image from the camera roll (rather than a new one from the camera) it will resave the image into the custom album, creating a new copy. Any chance of opening up the method to allow moving images to custom albums? Nick

Kjuly commented 11 years ago

Yes, the code now will leave the original image to the camera roll & copy it to the custom album. We've discussed at issue #8 . Let me see whether we can implement it. B.t.w., which lib did you use before, can you show me the link? Thanks! :)

sigmundfridge commented 11 years ago

I used the code from the original blog entry http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/

sigmundfridge commented 11 years ago

Also, issue 8 seems slightly different to me. I thought that was more 'when saving a new image it appears in both the Camera Roll and the Album'. I am fine with that behaviour. My issue is moving existing images to the custom album, which are then duplicated so you get two identical images on the camera roll (with the newer one appearing in the custom album as well).

Kjuly commented 11 years ago

um.. sorry, what do you mean of "moving existing images to the custom album"? Does it like this: Select an image at Camera Roll, then "Add To" the "Custom Photo Album"?

This will leave the original image at Camera Roll, just copy the image to the custom album. When you delete the image at Camera Roll, the copy in custom album will also be deleted. Reverse won't. (If you has Instagram App installed, you'll got a similar custom album named "Instagram").

Sorry if I misunderstood. But can you show me the full steps that what you want to do then. Thanks! :)

sigmundfridge commented 11 years ago

So I'm using UIImagePickerController to do two things. 1) Open the camera, take a picture and then save this to a custom album 2) Open a popupview, select an existing image (e.g. from the camera roll) and 'move' this to a custom album.

I believe this is what then happens in each case..... 1) The camera creates a new image, which is saved to both the camera roll and appears in the custom album i.e. the custom album is acting as a filter, showing certain images from the camera roll. Deleting the image from the camera roll also removes it from the custom album. 2) As I have selected an already existing image (i.e. one with an existing NSURL on the device) when I save the image it saves a new copy. Therefore a new image appears in the camera roll and also in the custom album. I have saved an entirely new copy 'as if' it was a new image created by the camera. The old copy (the one I selected from the camera roll) still exists. In the camera roll I now have the original image, and the duplicate. In the custom album 'filter' I just have the duplicate.

I originally modified the touch code magazine code to accept different completion and failure blocks. So when saving an image from the camera it would work much like the current github code (i.e. case 1). But I could also use the

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

message to add existing images to the custom album, without saving a new version of that image (as in case 2).

Hopefully this makes more sense. I can send you actual code if that will help.

Kjuly commented 11 years ago

I see. Thanks for your detail description! In the case 2, I think the reason image duplicated at Camera Roll is that this lib treat the copied image as a totally new one.

As you see, we use

[self writeImageToSavedPhotosAlbum:image.CGImage
                       orientation:(ALAssetOrientation)image.imageOrientation 
                   completionBlock:[self _resultBlockOfAddingToAlbum:albumName
                                                          completion:completion
                                                             failure:failure]];

to save image. After saving process succeed, it'll dispatch

// add the asset to the custom photo album
[self _addAssetURL:assetURL
           toAlbum:albumName
           failure:failure];

Seems it always treat the image as a new asset with a new assetURL. Um.. I think we do need to public -_addAssetURL:toAlbum:failure: & use this method directly with target image's assetURL to fix this issue.

I set -addAssetURL:toAlbum:withCompletionBlock:errorBlock: to be private & renamed it to _addAssetURL:toAlbum:failure: at 778b9cdfb088590ff4c3b13f0a6eafca07adaeb7, and did several modifications with following commits. I'll put it back! I'm glad that if you can enhance the lib & send a pull request to me. I'll merge it then. Thanks! :D

Kjuly commented 11 years ago

Note: The original method -addAssetURL:toAlbum:withCompletionBlock: is renamed to -addAssetURL:toAlbum:failure:. The param completionBlock is replaced with failure. Cause it finally uses

[self enumerateGroupsWithTypes:ALAssetsGroupAlbum 
                    usingBlock:... // block process to deal
                  failureBlock:... // the param block, it's completionBlock before, but use `failure` now

So I think the original one is inappropriate. You can do completion job in failure block either, just judge whether the error valid. The failure block is defined as

typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
sigmundfridge commented 11 years ago

That looks perfect to me. Thanks for the quick response. I haven't had chance to test it yet as I'm using the Cocoa Pods installation and that doesn't appear to have changed. How soon would the pod change, or shall I just manually install from the github source?

Kjuly commented 11 years ago

@sigmundfridge i'll manage it soon ;)

sigmundfridge commented 11 years ago

Awesome, thanks for all the help.

Kjuly commented 11 years ago

The latest version on CocoaPod is 1.0.3(878fcf4e7103f0349a27a684ba363f5855b0bdac). :)

sigmundfridge commented 11 years ago

I've found that the failure block only fires if adding the asset to the custom album fails.

From my understanding there are two main ways to use blocks. 1) have a single completion block and check if !error = nil 2) have a completion and failure block.

I don't understand the reasoning behind only having a failure block, as it doesn't fire if the asset was added successfully. In my case I want to dismiss the view controller if the asset is added successfully, but I have no easy way to do this (without notifications).

I might be missing something subtle but I would assume having the

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

as a public message, and then passing the errorBlock as the failureBlock in

 [self enumerateGroupsWithTypes:ALAssetsGroupAlbum 
                usingBlock:... // block process to deal
              failureBlock:... // the param block, it's completionBlock before, but use `failure` now

would be more useful?

Kjuly commented 11 years ago

I found that the original code uses completionBlock for both completion & failure cases. Seems we do need to add a completion block for -addAssetURL:toAlbum:failure:, and fire the completion block when [group addAsset: asset]; succeed.

Just fixed it now, please pull the latest code to test whether it runs as you want. Let me know if you've any question. Thanks! :)

sigmundfridge commented 11 years ago

Hi, I updated to 1.0.4 using cocoa pods, but the code seems the same. Do I need to do a manual install from the git repo?

Kjuly commented 11 years ago

Nope, v1.1.1 is available. v1.1.x has convert MRC to ARC now. ;)

sigmundfridge commented 11 years ago

Ok, I've had a little test and it seems to work almost perfectly. I may have found a bug, but I have some issues to sort out before I can be sure. Tentatively, this seemsto be the issue.... 1) Delete an app that has been approved to access the photos 2) Reinstall app. 3) Try to save an image (or move an image) to a custom album. 4) Blank custom album is created but save fails 5) Click save again and the picture is placed in the custom album.

As I say, this is tentative as I have other issues with my code to sort out first. I will report back.

Kjuly commented 11 years ago

Hi @sigmundfridge , can u create a new issue for it? I'll test & try to fix it when I'm free, thx! :)

sigmundfridge commented 11 years ago

Thanks for all the quick work. Hopefully, one day I'll be of more use than just finding bugs

Kjuly commented 11 years ago

@sigmundfridge you're welcome. I'm sure you will. ;)