33

次のコードを使用して、画像をカスタム アルバムに配置しようとしています。

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
}];

したがって、この行のユーザーログに多くのエラーが表示されますcreateAssetRequest.placeholderForCreatedAsset

お気に入り

1 CoreFoundation __exceptionPreprocess + 1245624

2 libobjc.A.dylib objc_exception_throw + 34136

3 枚の写真 __48-[PHChangeRequestHelper generateUUIDIfNecessary]_block_invoke + 116552

2 libdispatch.dylib _dispatch_semaphore_wait_slow + 79828

3枚の写真 -[PHChangeRequestHelper generateUUIDIfNecessary] + 115992

4 写真 -[PHAssetCreationRequest placeholderForCreatedAsset] + 244020

だから[PHChangeRequestHelper generateUUIDIfNecessary]私にクラッシュを与えてください。

これはiOS > 10でのみ表示され、シミュレーターでは再現できません。

それは何ですか?それを修正する方法は?

4

3 に答える 3

1

以下のコードを試してください。

__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;
        }];
    }
}];
于 2017-08-18T12:04:58.873 に答える