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

ios8.4 sometimes fail when addAssetURL to album #39

Closed AshStill closed 8 years ago

AshStill commented 9 years ago

this function sometime return nil, which will lead the saveimage to failureblock.

AshStill commented 9 years ago

I think in ios8 , PHPhotolibrary should be an option to use https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/index.html#//apple_ref/doc/uid/TP40014404

Kjuly commented 9 years ago

@AshStill yup, u do need to import Photos framework for ur project since iOS 8.

AshStill commented 9 years ago

@Kjuly I have imported Photos framework in my project and sometimes still fail when save asset to savedAlbum especially when updated to iOS8.4.1.

Kjuly commented 9 years ago

Hey @AshStill, what's the fail error that u got? I'll debug to c anything wrong w/ the new version when got time, pretty busy these days. sorry for that.

AshStill commented 9 years ago

@Kjuly I find an solution for these. I will make pull request once I've done.

AshStill commented 9 years ago

@Kjuly I think i finished with PHPhotoLibrary but the problem is that I can't return an assetUrl, because PHAsset has its own identifier but not assetUrl :sob:

Kjuly commented 9 years ago

@AshStill, sorry, still don't have time to take a look at it yet, will check it around this weekend. :)

Kjuly commented 9 years ago

Hi @AshStill , i tried on iOS 8.4.1, w/o any issue, even tried to delete the album first. Several questions need ur help to answer:

  1. While u're saving photo by the -saveImage:toAlbum:completion:failure:
    1.1. Was completion block invoked before u trying to show the photo? (Make sure the photo was saved exactally before using it, or u can use the cached original photo while in saving process.)
    1.2. Or the failure block is invoked? If it is, what's the detail error?
  2. What's the detail error u got in -assetForURL:resultBlock:failureBlock:'s failureBlock?

B.t.w., it'll be good if u offer some code snippets. :)

AshStill commented 9 years ago

@Kjuly In my case, in the following function, the asset is nil, so it leads to the failure block _assetForURLResultBlockWithGroup:assetURL:completion:failure:

AshStill commented 9 years ago

I use following code in ios8 in my case.

-(void)performSaveImage:(UIImage*)image toAlbum:(NSString*)name completionHandler:(void(^)(BOOL success, NSError *error))completionHandler
{
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat:@"title = %@" , name];
__block PHAssetCollection *album = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:options].firstObject;
if (!album)
{
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:name];
    } error:nil];
    album = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:options].firstObject;
}
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    // Request creating an asset from the image.
    PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    // Request editing the album.
    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];

    // Get a placeholder for the new asset and add it to the album editing request.
    PHObjectPlaceholder *assetPlaceholder = [createAssetRequest placeholderForCreatedAsset];
    [albumChangeRequest addAssets:@[ assetPlaceholder ]];

} completionHandler:^(BOOL success, NSError *error) {
    if (completionHandler) {
        completionHandler(success,error);
    }
}];
}
Kjuly commented 9 years ago

In my case, in the following function, the asset is nil, so it leads to the failure block _assetForURLResultBlockWithGroup:assetURL:completion:failure:

Which one, L157 or L181? And what's the assetURL u were trying to save?

B.t.w., can u try the latest code's demo? Let's c whether it works in ur environ.

Onion520 commented 6 years ago

YEP,I have the same question . when debug in L157 or L181 ,it works ok . without breakpoint it will filled in L181 [group addAsset:asset] with asset is nil Sorry for my poor English

Kjuly commented 6 years ago

@Onion520

without breakpoint it will filled in L181 [group addAsset:asset] with asset is nil

Seems asset cannot be found. What's the value of assetURL in L185 for ur case?

Onion520 commented 6 years ago

assetURL : assets-library://asset/asset.mov?id=C10AB3B5-EE07-4D12-A4DD-14E976FA854E&ext=mov

when put a breakpoint in L157 or L181 the move can be save ok . And This is only on my cell phone。 other phone didn't have the question @Kjuly

Onion520 commented 6 years ago

it aways field in L87 with asset is nil