简体   繁体   English

将图像从URL动态下载到UIImageVIew

[英]Downloading image from a url into a UIImageVIew dynamically

I am trying to insert and image into a UIImageView from a url. 我试图从URL中插入和映像到UIImageView。 I used the following code to do so. 我使用以下代码来执行此操作。 When running the program gets stuck at 运行程序时遇到困难

NSURL *url = [NSURL URLWithString:urlstring];

In the below code and it shows "Thread 1 : signal SIGABRT" on that particular line. 在下面的代码中,它在该特定行上显示“Thread 1:signal SIGABRT”。 Can someone help me with this and tell if the format that i used is correct or what i had did wrong?? 有人可以帮助我,并告诉我使用的格式是否正确或我做错了什么?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"newoffer";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil)
{
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *temp = [product objectAtIndex:indexPath.row];
UILabel *Label = (UILabel *)[cell viewWithTag:201];
Label.text = [temp objectForKey:@"item_name"];
UIImageView *Image = (UIImageView *)[cell viewWithTag:200];
NSString *urlstring=[temp objectForKey:@"image_url"];
NSURL *url = [NSURL URLWithString:urlstring];
NSData *data = [NSData dataWithContentsOfURL:url];
Image.image = [UIImage imageWithData:data];

return cell;

}

Change this code: 更改此代码:

NSURL *url = [NSURL URLWithString:urlstring];
NSData *data = [NSData dataWithContentsOfURL:url];
Image.image = [UIImage imageWithData:data];

To: 至:

 dispatch_queue_t myqueue = dispatch_queue_create("myqueue", NULL);

    // execute a task on that queue asynchronously
    dispatch_async(myqueue, ^{
NSURL *url = [NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];;
NSData *data = [NSData dataWithContentsOfURL:url];
 dispatch_async(dispatch_get_main_queue(), ^{
Image.image = [UIImage imageWithData:data]; //UI updates should be done on the main thread
     });
    });

As mentioned by others, an Image Caching library like SDWebImage will help a lot because even with this implementation, you just push the download process to a background thread so the UI does not get stucked, but you are not caching anything. 正如其他人所提到的,像SDWebImage这样的图像缓存库会有很大的帮助,因为即使使用这个实现,你只需将下载过程推送到后台线程,这样UI就不会被卡住,但你不会缓存任何东西。

Try this 尝试这个

[image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.vbarter.com/images/content/1/9/19517.jpg"]]]];

For async downloading 用于异步下载

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

[image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.vbarter.com/images/content/1/9/19517.jpg"]]]];

});

if url is dynamic then 如果url是动态的话

NSString *stringUrl; // this can be any valid url as string

[image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:stringUrl]]]];

NSData *data = [NSData dataWithContentsOfURL:url];

will load imageData synchronous, that means main thread will be blocked . 将加载imageData同步,这意味着主线程将被阻止。

Use the project on github: SDWebImage for image asynchronous loading and cache. 使用github上的项目: SDWebImage进行图像异步加载和缓存。

There's probably better libraries that do this now, but I have always been using this for my projects and it works well: AsyncImageView . 现在可能有更好的库可以做到这一点,但我一直在为我的项目使用它,它运行良好: AsyncImageView There are other alternatives like SDWebImage 还有其他替代方案,如SDWebImage

But basically, you don't want to use 但基本上,你不想使用

NSData *data = [NSData dataWithContentsOfURL:url];

because it will block the main thread until the image is downloaded. 因为它会阻止主线程直到下载图像。 To avoid this you might want to use something asynchronous, like the above two libraries. 为避免这种情况,您可能希望使用异步方法,例如上面的两个库。

For example, with AsyncImageView , it becomes as easy as: 例如,使用AsyncImageView ,它变得如此简单:

myImageView.imageURL = someNSURL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM