短视频平台开发,将图片、视频保存到本地的相册中实现的相关代码
获取本地相册
- (IBAction)goodsButton1Touch:(id)sender {
//拿到获取相册的权限
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
UIImagePickerController *pic = [[UIImagePickerController alloc] init];
pic.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
pic.delegate = self;
[self presentViewController:pic animated:YES completion:nil];
}
}
//点击相片后会跑这个方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//拿到图片会就销毁之前的控制器
[picker dismissViewControllerAnimated:YES completion:nil];
//info中就是包含你在相册里面选择的图片(info字典里面还有图片的url-有需要的可以将info打印出来取自己要的数据)
UIImage *image = info[UIImagePickerControllerOriginalImage];
[self.goodsImage1 setImage:image];
}
存进本地相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
//第三个参数可以执行回调,但是一般执行这个方法
//contextInfo 为函数的第四个参数,设置完可以在回调中调用到
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存完成");
}
截图
//开启上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,0);
//获取当前的图片类型上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//截图 把view放到上下文
[self.view.layer renderInContext:ctx];
//关闭上下文
UIGraphicsEndImageContext();
保存视频
//#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(@"未能保存视频到相册");
}
}];
以上就是短视频平台开发,将图片、视频保存到本地的相册中实现的相关代码, 更多内容欢迎关注之后的文章