ios - PHPhotoLibrary se bloqueó al obtener placeholder ForCreatedAsset
objective-c xcode (2)
Intento poner la imagen en el álbum personalizado con este código:
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
}];
Por lo tanto, recibo muchos errores en los registros de los usuarios en esta fila createAssetRequest.placeholderForCreatedAsset
me gusta
1 CoreFoundation __exceptionPreprocess + 1245624
2 libobjc.A.dylib objc_exception_throw + 34136
3 fotos __48- [PHChangeRequestHelper generarUUIDIfNecessary] _block_invoke + 116552
2 libdispatch.dylib _dispatch_semaphore_wait_slow + 79828
3 fotos - [PHChangeRequestHelper generarUUIDIfNecessary] + 115992
4 fotos - [PHAssetCreationRequest placeholderForCreatedAsset] + 244020
por lo que [PHChangeRequestHelper generateUUIDIfNecessary]
me falla.
Veo esto solo en iOS> 10 , y no puedo reproducirlo en el simulador.
¿Qué es? ¿Cómo arreglar eso?
Por favor intente código debajo.
__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;
}];
}
}];
Puede utilizar el siguiente Código.
`#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);
}
}];
}`
Aquí MKCustomPhotoAlbumName es la macro y coloca el nombre del álbum que querías crear
Como puede observar a partir del código, su imagen se guarda en el álbum creado habitualmente en la aplicación de fotos iPhone. y si ya está creado, se guarda directamente en él.