简体   繁体   English

使用URL设置UIImageView

[英]Set UIImageView using URL

I am setting image in UIImageView using: 我使用以下方法在UIImageView中设置图像:

[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"random_image"]];

I am able to see the image on phone as expected. 我能够按预期在电话上看到图像。

However, if I use the method without placeholder argument 但是,如果我使用不带占位符参数的方法

[self.imageView setImageWithURL:[NSURL URLWithString:urlString]];

OR pass the placeholderImage as nil 或将占位符图像传递为nil

[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];

then the image is not set and my imageView is blank. 然后未设置图像,并且我的imageView为空白。 I understand that placeholder image is for " representing the current state of the asset suitable for temporarily displaying in your extension's UI. ". 我知道占位符图像用于“ 代表适合临时显示在扩展程序UI中的资产的当前状态。 ”。

I don't understand how its absence should impact me the way it is impacting. 我不了解它的缺失会如何影响我。 Any pointers? 有指针吗?

If you set default placeholder image then imageview gets width and night.Otherwise imageview frame is may be (0,0). 如果您设置了默认的占位符图像,那么imageview就会获得宽度和夜晚,否则imageview的帧可能是(0,0)。

Or 要么

The image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view 应该使用NSURLConnection异步下载图像,并且只有完全下载后,才应将其转换为UIImage并分配给图像视图

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]]队列:[NSOperationQueue mainQueue] complementHandler:^(NSURLResponse *响应,NSData * data,NSError *错误){

ImageViewName.image = [UIImage imageWithData:data];

}]; }];

I found the issue. 我发现了问题。 I added an image view in UITableViewCell with the name imageView . 我在UITableViewCell中添加了一个名为imageView的图像视图。 Apparently, UITableViewCell already has a property with the same name. 显然,UITableViewCell已经具有相同名称的属性。 I changed the name of myImageView to cellImageView and now, I don't need the placeholder parameter anymore! 我将myImageView的名称更改为cellImageView,现在,我不再需要占位符参数! Having said that, I still don't understand what happened behind the screens! 话虽如此,我还是不明白屏幕后面发生了什么!

i hope these code will help u.... 我希望这些代码对您有帮助。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{

        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                                 [NSURL URLWithString:@“image URL”]]];
        dispatch_sync(dispatch_get_main_queue(), ^{

                [[cell myimage] setImage:image];

            }
         });
    });

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

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