Reputation: 8066
The url is:
https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com
I want to get above image into to a UIImage. I tried this:
NSURL *urlString = [NSURL URLWithString:@"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com"];
NSData *imageData = [[NSData alloc]initWithContentsOfURL:urlString];
UIImage *img = [UIImage imageWithData:imageData];
But it seems this doesn't work. The url doesn't have an file extension like .png
or .jpg
. Is is possible to extract the image from this kind of URL?
Upvotes: 1
Views: 1756
Reputation: 130183
You were close, what you're going to want to do is UIImagePNGRepresentation
in the case of this particular image (because the image you linked is actually .png
) to basically convert the data to a workable image. If you are trying to use a .jpg
image use UIImageJPEGRepresentation
.
NSURL *urlString = [NSURL URLWithString:@"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:urlString]])];
UIImage *image = [UIImage imageWithData:imageData];
Or, if you're as big of a fan on one-liners as I am, you can use this:
UIImage *image = [UIImage imageWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com"]]])];
Upvotes: 4