ios what PHPhotoLibrary crashed when getting placeholderForCreatedAsset




what is phasset ios (2)

Please try below code.

__block NSString* tempPath;
// Add it to the photo library
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

    if (self.assetCollection) {
        PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
        [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
    }
    tempPath = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];
} completionHandler:^(BOOL success, NSError *error) {
    PHFetchResult* assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[tempPath] options:nil];
    if (!success) {
        NSLog(@"Error creating asset: %@", error);
    } else {
        PHFetchResult* assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[tempPath] options:nil];
        PHAsset *asset = [assetResult firstObject];
        [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
            UIImage* newImage = [UIImage imageWithData:imageData];
            self.imgView.image = newImage;
        }];
    }
}];

I try to put image to custom album with this code:

PHAssetCollection *album = [self getMyAlbum];
UIImage *image = [self getMyImage];

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

    PHObjectPlaceholder * placeHolder = createAssetRequest.placeholderForCreatedAsset;

    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];

    if(placeHolder){
        [albumChangeRequest addAssets:@[ placeHolder ]];
    }

} completionHandler:^(BOOL success, NSError *error) {
    //doesen't matter
}];

So, i get many errors in users logs in this row createAssetRequest.placeholderForCreatedAsset

like

1 CoreFoundation __exceptionPreprocess + 1245624

2 libobjc.A.dylib objc_exception_throw + 34136

3 Photos __48-[PHChangeRequestHelper generateUUIDIfNecessary]_block_invoke + 116552

2 libdispatch.dylib _dispatch_semaphore_wait_slow + 79828

3 Photos -[PHChangeRequestHelper generateUUIDIfNecessary] + 115992

4 Photos -[PHAssetCreationRequest placeholderForCreatedAsset] + 244020

so [PHChangeRequestHelper generateUUIDIfNecessary] give me crash.

I see this only on iOS > 10, and I can't reproduct this in the simulator.

What is it? How to fix that?


you can use the Following Code.

`#pragma mark - Create Custom Album name

-(void)saveImageWithAlbumNameAndImage:(UIImage *)image
{
__block PHFetchResult *photosAsset;
__block PHAssetCollection *collection;
__block PHObjectPlaceholder *placeholder;

// Find the album
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", MKCustomPhotoAlbumName];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                      subtype:PHAssetCollectionSubtypeAny
                                                      options:fetchOptions].firstObject;
// Create the album
if (!collection)
{
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:MKCustomPhotoAlbumName];

        placeholder = [createAlbum placeholderForCreatedAssetCollection];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success)
        {
            PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier]
                                                                                                        options:nil];
            collection = collectionFetchResult.firstObject;
        }
    }];
}

// Save to the album
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    placeholder = [assetRequest placeholderForCreatedAsset];
    photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection
                                                                                                                  assets:photosAsset];
    [albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
    if (success)
    {
        NSString *UUID = [placeholder.localIdentifier substringToIndex:36];
        NSLog(@"UUID : %@",UUID);
    }
    else
    {
        NSLog(@"%@", error);
    }
}];
}`

Here MKCustomPhotoAlbumName is the Macro and Placed the Name of the Album you wanted to create

As you can observe from the code that Your Image is saved into customary created Album in the photos app iPhone. and if it is Already created then saved directly into it







phphotolibrary