gpt4 book ai didi

ios - 从网络动态加载图像到用户界面

转载 作者:行者123 更新时间:2023-11-29 01:13:31 25 4
gpt4 key购买 nike

我必须下载图像并将其存储在 NSMutableArray 中。然后我必须稍后访问它并显示

我正在使用以下代码将图像下载到文档文件夹,

NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentDir stringByAppendingPathComponent:@"GoogleLogo.png"];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com/images/srpr/logo11w.png"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Download Error:%@",error.description);
}
if (data) {
[data writeToFile:filePath atomically:YES];
NSLog(@"File is saved to %@",filePath);
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",filePath]];
// NSLog(@"sddsd %@",fileName);
}
}];

如何将所有下载的图像添加到 NSMutableArray 并稍后访问它?

最佳答案

尝试使用 SDWebImage从网络动态设置图像到 ImageView 的类

将以下文件导入您的头文件。

导入“UIImageView+WebCache.h”

导入文件后尝试以下代码。

[self.imgLogo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[self.dictTemp valueForKey:@"camp_image"]]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];

一般用来缓存图片,方便以后访问

关于ios - 从网络动态加载图像到用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35499330/

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