开发者

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

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 = [NSString alloc];
[temp stringwithString:@"http://192.168.1.2x0/pic/LC.jpg"]
temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
    nil,
    (CFStringRef)temp,                     
    NULL,
    NULL,
    kCFStringEncodingUTF8)
autorelease];开发者_开发百科


NSData *dato = [NSData alloc];
 dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
 pic = [pic initWithImage:[UIImage imageWithData:dato]];

This code is in viewdidload of the view but nothing is displayed! The server is working because i can load xml files from it. but i can't display that image!

I need to load the image programmatically because it has to change depending on the parameter passed! Thank you in advance. Antonio


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];


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


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.


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];


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜