How to display image on UIImageView using URL and NSString in ios?

后端 未结 10 1994
孤城傲影
孤城傲影 2021-01-02 16:49

I want to display image on UIImageView using URL and NSString. I am unable to show image.

My code is:

UIImageView *view_Im         


        
10条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 17:21

    Just do it

    NSString *imgURL = @"imagUrl";
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
    
    [YourImgView setImage:[UIImage imageWithData:data]];
    

    Using GCD : If you don't want to hang your application then you can download your image in background.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *imgURL = @"imagUrl";
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
    
        //set your image on main thread.
        dispatch_async(dispatch_get_main_queue(), ^{
            [YourImgView setImage:[UIImage imageWithData:data]];
        });    
    });
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题