iOS 保存图片视频到手机相册

    //保存相册

 

 

UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image,__nullableid completionTarget,__nullableSEL completionSelector,void *__nullable contextInfo)__TVOS_PROHIBITED;//xcode中的方法

 

    UIImageWriteToSavedPhotosAlbum(getImage, self, @selector(image:didFinishSavingWithError:contextInfo:),@"hello word");

 

 

//保存相册方法(必须是这个方法,其他方法不行)

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

{

    NSLog(@"保存完毕 %@",contextInfo);

}

 

============保存视屏到相册一:这个方法在9.0 后遗弃

 

1在工程中添加这个框架:

#import <AssetsLibrary/AssetsLibrary.h>

2

    NSBundle *bundle=[NSBundle mainBundle];

    NSString *videoPath=[bundle pathForResource:@"test" ofType:@"mp4"];

    videoPath是你视频文件的路径,我这里是加载工程中的

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath]

                                completionBlock:^(NSURL *assetURL, NSError *error) {

                                    if (error) {

                                        NSLog(@"Save video fail:%@",error);

                                    } else {

                                        NSLog(@"Save video succeed.");

                                    }

                                }];

 

===============保存视频到相册二:

 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {       

    NSString* path = [[info objectForKey:UIImagePickerControllerMediaURL] path];

    // 保存视频

// movie的路径
NSURL*url = info[UIImagePickerControllerMediaURL];
 // movie是否可以保存到相册
BOOL canbesaved =UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([url path]);
  if (canbesaved) {
      
//将movie保存到相
UISaveVideoAtPathToSavedPhotosAlbum(url.path,self,@selector(videoSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
   }else{
NSLog(@"录像不能保存到相册");
  }

    UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);

    [self dismissModalViewControllerAnimated:YES];

 }

 // 视频保存回调

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo {

if(!error) {

NSLog(@"视频保存成功");

}else{

NSLog(@"视频保存不成功,error:%@",error);

}

}

 

保存视频到相册三:

#import <Photos/Photos.h>

    NSString *path=[[NSBundle mainBundle] pathForResource:@"test.MOV" ofType:nil];
            PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
            [photoLibrary performChanges:^{
                [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL
            fileURLWithPath:path]];
            } completionHandler:^(BOOL success, NSError * _Nullable error) {
                if (success) {
                    NSLog(@"已将视频保存至相册");
                } else {
                    NSLog(@"未能保存视频到相册");
                }
            }];

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值