gpt4 book ai didi

ios - 从 web 下载图像到照片应用程序 AFNetworking 3

转载 作者:行者123 更新时间:2023-11-29 12:03:52 24 4
gpt4 key购买 nike

我正在尝试将带有进度的图像从网络服务器直接保存到照片应用程序。这是我目前使用的,没有任何进展:

NSURLSessionDownloadTask *dl = [[NSURLSession sharedSession]
downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
if ( !error )
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
completionBlock(YES,image);
} else{
completionBlock(NO,nil);
}

}
];
[dl resume];

我知道我可以使用 NSURLSessionDownloadDelegate 来获取下载进度,但我想使用 AFNetworking 下载图像并在下载时获取进度。我知道 AFNetworking 有一种方法可以在 imageview 中显示图像,例如

[postCell.iv_postImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[[Utilities sharedUtilities] basePath],[item.imagePath substringFromIndex:2]]]];

但是有没有类似的方法来下载图片?

最佳答案

试试这个:-

 NSURLSessionConfiguration *configuration =[NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = your_url_here;
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);

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

[library writeVideoAtPathToSavedPhotosAlbum:URL completionBlock:^(NSURL *assetURL, NSError *error) {

if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error in Downloading video "message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok"otherButtonTitles:nil];
[alertView show];

} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Downloading video success....!!!"message:[error localizedDescription]delegate:nil cancelButtonTitle:@"Ok"otherButtonTitles:nil];
[alertView show];
}

}];

}];

[downloadTask resume];

关于ios - 从 web 下载图像到照片应用程序 AFNetworking 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35765868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com