美文网首页很常
iOS删除照片代码

iOS删除照片代码

作者: shier | 来源:发表于2021-02-20 11:09 被阅读0次

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

    相关文章

      网友评论

        本文标题:iOS删除照片代码

        本文链接:https://www.haomeiwen.com/subject/hrutfltx.html