1.保存图片到相册
//image是要保存的图片
- (void) saveImage:(UIImage *)image{
if (image) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
};
}
//保存完成后调用的方法
- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo {
if (error) {
NSLog(@"保存图片出错%@", error.localizedDescription);
}
else {
NSLog(@"保存图片成功");
}
}
2.保存视频到相册
//videoPath为视频下载到本地之后的本地路径
- (void)saveVideo:(NSString *)videoPath{
if (_videoPath) {
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([_videoPath path])) {
//保存相册核心代码
UISaveVideoAtPathToSavedPhotosAlbum([_videoPath path], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
//保存视频完成之后的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"保存视频失败%@", error.localizedDescription);
}
else {
NSLog(@"保存视频成功");
}
}
网友评论
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error) {
保存失败
}else{
保存成功
}
找了好久才把这个错误纠正,下载视频保存然后成功了,不过还是要感谢楼主
}
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
LSLog(@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
//设置下载路径,并将文件写入沙盒,最后返回NSURL对象
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
LSLog(@"path = %@",path);
return [NSURL fileURLWithPath:path];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
// 这里的filePath就是保存路径
LSLog(@"filePath = %@",filePath);
LSLog(@"错误了 = %@",error.description);
}];
[task resume];
//这是我用afn时设置下载路径的方式,那个filePath就是要用的保存路径