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

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


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.