iOS删除照片代码
/// 删除照片,按照时间倒序
/// @param count 数量
- (void)clearPhotos:(NSInteger)count {
PHFetchOptions *options = [[PHFetchOptions alloc] init];
//yes为顺序,no为倒序
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options.includeHiddenAssets = YES;
options.includeAllBurstAssets = YES;
options.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary|PHAssetSourceTypeCloudShared|PHAssetSourceTypeiTunesSynced;
options.fetchLimit = count;
PHFetchResult *fetchRequest = [PHAsset fetchAssetsWithOptions:options];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
NSMutableArray *assets = [NSMutableArray array];
[fetchRequest enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
BOOL b = [obj canPerformEditOperation:PHAssetEditOperationDelete];
if (b) {
[assets addObject:obj];
}
}];
if ([assets count]>0) {
[PHAssetChangeRequest deleteAssets:assets];
}
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"success");
}else {
NSLog(@"error");
}
}];
}
网友评论