Extract image from URL - objective-c

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?

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

Related

Unable to fetch data from nsurl

NSURL *url = [NSURL URLWithString:[self.imageArray objectAtIndex:indexPath.row]];
NSLog(#"My URL : %#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(#"New Data : %#",data);
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
cell.imageView.image = tmpImage;
i think i got your problem, check this like my upload image in your project.
i also make a gif about how to add this setting in your project , just follow the step in my gif.

Load url from array to ImageView in Xcode

I need load url from array (arrayCoverURL) to ImageView.
NSURL *imageURL = [NSURL URLWithString:[arrayCoverURL objectAtIndex:indexPath.item]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
cell.imageView.image = [UIImage imageWithData:imageData];

Adjusting UIImageView size and location based on the size of image fetched from service

In my iOS7 app I have view just like profile page in any social networking site. I have a profile image in top left corner. I am fetching the image from service url and loading it to a UIImageview.
My problem is, I have to increase the size of this image view when the size of the image from service is big. And also i have to adjust other UILabels beside this image view. I am using this code to get the size of image from url:
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
CGSize size = img.size;
I don't know initWithData:cache: method on UIImage class, is this category thing?
Anyway you can make UIImageView dimension to fit content UIImage by [imageView sizeToFit].
If you want to adjust scale, use [UIImage imageWithData:data scale:scale].
NSString *path = #"http://www.example.com/example#x2.png"
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data scale:2.0];
_imageView.image = img;
[_imageView sizeToFit];

Load image from plist

I am trying to input the link of the image on one of the plist <string>.
What code should I add in to the implementation in order to load the image from that website and display in an UIImage?
This should work:
NSData *imgData = [[NSData alloc] initWithContentsOfURL:urlFromPlist];
UIImage *img = [UIImage imageWithData:imgData];
[img drawInRect:someRect];

Improve speed - UIImage from NSURL

I'm using the following code to fetch some images, that are going into a tableview. But it takes ages (5-6 seconds) to get the 30 images in.
Is there a smarter/faster way to do this?
NSString *imageUrl = ......;
NSString *urlStr =
[imageUrl UrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *path = [[NSString alloc] initWithFormat:#"http://xxx.dk/xml.aspx%#", urlStr];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
return img;
[..... release];
Here profileimg is an get from Api ,String name
NSString *profileimg=[Check objectForKey:#"IMAGE"];
imageview.image=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profileimg]]];
I'd rather download every new image in array of images when a cell appears on the screen and just display already saved image if a cell appears on the screen for the second time (i.e. image for this cell has been already downloaded).
use asynchronize loading of image.
check out this sample code : http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html