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

前端 未结 5 1774
忘了有多久
忘了有多久 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:06

    It should be:

    NSURL * imageURL = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"];
    NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage * image = [UIImage imageWithData:imageData];
    

    Once you have the image variable, you can throw it into a UIImageView via it's image property, ie:

    myImageView.image = image;
    //OR
    [myImageView setImage:image];
    

    If you need to escape special characters in your original string, you can do:

    NSString * urlString = [@"http://192.168.1.2x0/pic/LC.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURL * imageURL = [NSURL URLWithString:urlString];
    ....
    

    If you're creating your UIImageView programmatically, you do:

    UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
    [someOtherView addSubview:myImageView];
    [myImageView release];
    
    0 讨论(0)
  • 2020-12-12 17:06

    And because loading image from URL takes ages, it would be better to load it asynchronously. The following guide made it stupid-simple for me:

    http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation

    0 讨论(0)
  • 2020-12-12 17:07

    This is the actual code.

    UIImageView *myview=[[UIImageView alloc]init];
    
        myview.frame = CGRectMake(0, 0, 320, 480);
    
        NSURL *imgURL=[[NSURL alloc]initWithString:@"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];
    
        NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];
    
        UIImage *image=[[UIImage alloc]initWithData:imgdata];
    
        myview.image=image;
    
        [self.view addSubview:myview];
    
    0 讨论(0)
  • 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;
         });
    });
    
    0 讨论(0)
  • 2020-12-12 17:33

    Try this

    NSURL *url = [NSURL URLWithString:@"http://192.168.1.2x0/pic/LC.jpg"];
     NSData *data = [NSData dataWithContentsOfURL:url];
    UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
    [subview setImage:[UIImage imageWithData:data]]; 
    [cell addSubview:subview];
    [subview release];
    

    All the Best.

    0 讨论(0)
提交回复
热议问题