Xcode iPhone Programming: Loading a jpg into a UIImageView from URL

前端 未结 5 1776
忘了有多久
忘了有多久 2020-12-12 16:35

My app has to load an image from a http server and displaying it into an UIImageView
How can i do that??
I tried this:

NSString *temp = [NSStrin         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 17:14

    You should load the image in the background or the view will freeze. Try this:

    UIImage *img = [[UIImage alloc] init];
    
    dispatch_async(dispatch_get_global_queue(0,0), ^{
    
        NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.test.com/test.png"];
        img = [UIImage imageWithData: data];
    
         dispatch_async(dispatch_get_main_queue(), ^{
            //PUT THE img INTO THE UIImageView (imgView for example)
            imgView.image = img;
         });
    });
    

提交回复
热议问题