//保存相册
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(@"未能保存视频到相册");
}
}];