1.保存網(wǎng)絡(luò)圖片到手機(jī)相冊(cè)
- (void)toSaveImage:(Nsstring *)urlString {
NSURL *url = [NSURL URLWithString: urlString];
SDWebImageManager *manager = [SDWebImageManager sharedManager]; UIImage *img;
if ([manager diskImageExistsForURL:url]) {
img = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
} else { //從網(wǎng)絡(luò)下載圖片
NSData *data = [NSData dataWithContentsOfURL:url];
img = [UIImage imageWithData:data];
}
// 保存圖片到相冊(cè)中
UIImageWriteToSavedPhotosAlbum(img,self,@selector(image:didFinishSavingWithError:contextInfo:), nil );
}
//保存圖片完成之后的回調(diào)
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
// Was there an error?
if (error != NULL) {
// Show error message…
NSLog(@"圖片保存失敗");
} else { // Show message image successfully saved
NSLog(@"圖片保存成功");
}
}
2.保存網(wǎng)絡(luò)視頻到手機(jī)相冊(cè)
- (void)playerDownload:(NSString *)url{
NSLog(@"urlurlurlurl===%@",url);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"jaibaili.mp4"];
NSURL *urlNew = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:urlNew];
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"%@",response);
[self saveVideo:fullPath];
}];
[task resume];
}
//videoPath為視頻下載到本地之后的本地路徑
- (void)saveVideo:(NSString *)videoPath{
if (videoPath) {
NSURL *url = [NSURL URLWithString:videoPath];
BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([url path]);
if (compatible) {
//保存相冊(cè)核心代碼
UISaveVideoAtPathToSavedPhotosAlbum([url path], self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
}
}
}?
//保存視頻完成之后的回調(diào)
- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo {
if (error) {
NSLog(@"保存視頻失敗%@", error.localizedDescription);
[self showHintMiddle:@"視頻保存失敗"];
} else {
NSLog(@"保存視頻成功");
}
}