gpt4 book ai didi

ios - objective c - 从网络下载图像并使用它在 ImageView 中显示的有效方法

转载 作者:行者123 更新时间:2023-11-28 19:04:35 27 4
gpt4 key购买 nike

在我的应用中假设有 2 个 View ViewA 和 ViewB

在 ViewA 中有供用户选择选项的按钮。如果他推送其中一个,我将通过网络服务从网络中提取一些图像并将它们下载到用户的机器上,我也会将它们的路径放入一个数组中。

然后在 ViewB 中,我想从该数组中获取图像并在 ImageView 中显示它们

这是我下载图片的方式

-(void)startDownload
{
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:@"http://xxxx.com/Tulips.jpg"];
[arr addObject:@"http://xxxx.com/Koala.jpg"];
[arr addObject:@"http://xxxx.com/Penguins.jpg"];
for (int i=0; i<[arr count]; i++) //download array have url links
{
NSURL *URL = [NSURL URLWithString:[arr objectAtIndex:i]];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:URL];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if([data length] > 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
//make your image here from data.
UIImage *imag = [[UIImage alloc] initWithData:[NSData dataWithData:data]];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array objectAtIndex:0];
NSString *imgstr=[NSString stringWithFormat:@"%d",i];
NSString *pngfilepath = [NSString stringWithFormat:@"%@sample%@.jpg",docDir,imgstr];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(imag)];
[data1 writeToFile:pngfilepath atomically:YES];


img = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:pngfilepath]];

NSLog(@"file is written");

}
else if ([data length] == 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
NSLog(@"No Data!");
}
else if (![[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"]){
NSLog(@"Error = %@", error);
}
}];

}



}

当我运行该应用程序时,我看到文件已写入日志正在运行,所以我认为下载图像是成功的,但我无法在 imageview 中显示图像

您可能会认为商店中的测验应用程序可以清楚地了解我的问题。测验首先下载问题的图像,然后在另一个 View 中使用它们。这正是我想要的。

如果我的下载代码是正确的,我该如何显示它们?

最佳答案

此代码将允许您从 Web 下载图像,并且不需要将图像保存在文档目录中:

 NSMutableArray *arry = [[NSMutableArray alloc] init];
[arry addObject:@"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRr0WK-Q2t4Xxr1b6Kl7-lXdVEIh_Hj3HiDXk--Qg_0UAY0Y96P6w"];
[arry addObject:@"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRr0WK-Q2t4Xxr1b6Kl7-lXdVEIh_Hj3HiDXk--Qg_0UAY0Y96P6w"];
[arry addObject:@"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRr0WK-Q2t4Xxr1b6Kl7-lXdVEIh_Hj3HiDXk--Qg_0UAY0Y96P6w"];

for (int i=0; i<[arry count]; i++) //download array have url links
{
NSString *string=[arry objectAtIndex:i];
NSURL *url=[NSURL URLWithString:string];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
UIImage *imagemain=[UIImage imageWithData:data];
// CGSize size=imagemain.size;
// UIImage *compimage=[appdel resizeImage:imagemain resizeSize:CGSizeMake(45,45)];
//
// Cell.imgProfile.image=compimage;
// // CGSize size1=compimage.size;
imageView.image=imagemain;
}];

}

关于ios - objective c - 从网络下载图像并使用它在 ImageView 中显示的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677338/

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