iOS保存圖片和視頻到相冊

1.保存圖片到相冊

OC

 //image是要保存的圖片
- (void) saveImage:(UIImage *)image{

    if (image) {

        UIImageWriteToSavedPhotosAlbum(image, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);

    };

}
//保存完成后調(diào)用的方法
- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo {
    if (error) {
        NSLog(@"保存圖片出錯(cuò)%@", error.localizedDescription);
    }
    else {
        NSLog(@"保存圖片成功");
    }
}

Swift

     func saveImage(image: UIImage) {
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
    }

     @objc func image(image: UIImage,didFinishSavingWithError: NSError?,contextInfo: AnyObject)  {
        if didFinishSavingWithError == nil {
           //保存成功
        }else{
            //保存失敗
        }
    }

2.保存視頻到相冊

OC

//videoPath為視頻下載到本地之后的本地路徑
- (void)saveVideo:(NSString *)videoPath{

    if (_videoPath) {

            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([_videoPath path])) {
                //保存相冊核心代碼
                UISaveVideoAtPathToSavedPhotosAlbum([_videoPath path], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
            }
        
    }

}
//保存視頻完成之后的回調(diào)
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存視頻失敗%@", error.localizedDescription);
    }
    else {
        NSLog(@"保存視頻成功");
    }
  
}

Swift

    func saeVideo(videoPath: String) {
        if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath) {
            UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, #selector(video(videoPath: didFinishSavingWithError:contextInfo:)), nil)
        }
    }
    
    @objc func video(videoPath: String,didFinishSavingWithError: NSError?,contextInfo: AnyObject)  {
        if didFinishSavingWithError == nil {
            //保存成功
        }else{
            //保存失敗
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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