Store an image using UIImage from NSURL
USing UIImage, I know we can store an im开发者_JAVA技巧age from a URL. But I am currently stuck at some point where its not working for me. I am trying to grab the image from the URL as shown below:
NSString *filename = @"12121212"
UIImage *logoImage = [UIImage imageNamed:[NSString stringWithFormat:@"http://www.example.com/devlopment.cfm?method=%@",fileName]];
NSURL *url = [NSURL URLWithString: logoImage];
I am sure, I am getting the image but cannot load it.
Am I going the correct way?
Sagos
You'll need to use UIImage
's +imageWithData:
method and NSData
's +dataWithContentsOfURL:
method:
NSString *fileName = @"12121212";
NSString *filePath = [NSString stringWithFormat:@"http://www.example.com/devlopment.cfm?method=%@",fileName];
UIImage *im = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]]];
In your example, you have things in a sort of backwards order. Setting UIImage *logoImage
should be the last part. Additionally, turning a URL into an image is sort of a two step process.
First you need to get the data:
NSString *filename = @"12121212";
NSURL *tempURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/devlopment.cfm?method=%@",fileName]];
NSData *tempData = [NSData dataWithContentsOfUrl:tempURL];
Now you need to make it into an image:
UIImage *logoImage = [UIImage imageWithData:tempData];
I'm sure this code leaks and it doesn't handle network problems but you get the picture.
精彩评论