iOS 幾個常用保存視頻到相冊的方法

目前知道的三種方法可以保存視頻至本地相冊:
方法一:iOS3 之后一直可以使用,兼容性極強;
方法二:支持 iOS9 以前,在 iOS9 之后被棄用;
方法二:在 iOS8 之后可以使用,一直沿用至今。

1、UIKIT_EXTERN 全局方法

BOOL videoCompatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(item.savePath);
//檢查視頻能否保存至相冊
if (videoCompatible) {
    UISaveVideoAtPathToSavedPhotosAlbum(item.savePath, self,
@selector(video:didFinishSavingWithError:contextInfo:), nil);
} else {
    NSLog(@"該視頻無法保存至相冊");
}
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存視頻失敗:%@", error);
    } else {
        NSLog(@"保存視頻成功");
    }
}

2、ALAssetsLibrary(iOS 9 廢棄)

//#import <AssetsLibrary/AssetsLibrary.h>
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:item.savePath]
                            completionBlock:^(NSURL *assetURL, NSError *error) {
                                if (error) {
                                    NSLog(@"Save video fail:%@",error);
                                } else {
                                    NSLog(@"Save video succeed.");
                                }
                            }];

3、PHPhotoLibrary(iOS 8 支持)

//#import <Photos/Photos.h>
PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
[photoLibrary performChanges:^{
    [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
fileURLWithPath:item.savePath]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
    if (success) {
        NSLog(@"已將視頻保存至相冊");
    } else {
        NSLog(@"未能保存視頻到相冊");
    }
}];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容