Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retina display for an image from URL

I have some images I need to get from the web. Just using data from a URL.
They need to show correctly on Retina Display.
When I get the images from the web, they still look pixelated. I need to set the images' scale to retina display (2.0), but I must be missing something. Here's what I did so far.

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:@"http://www.msdomains.com/tmp/test.png"];

CGRect labelFrame = CGRectMake(0,0,64,64);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:labelFrame];
imageView.contentScaleFactor = [UIScreen mainScreen].scale;

[imageView setImage:img];
[self addSubview:imageView];
[imageView release];
like image 393
Ted Avatar asked Jul 15 '12 12:07

Ted


People also ask

How do I create a retina image for my website?

To make your website retina-ready, use Scalable Vector Graphics (SVG) whenever possible. SVG is an XML-based graphic format that presents images in high quality. SVG images can be viewed in Internet browsers that use XML or be displayed on mobile phones in SVGB or SVGT file formats.

What is retina image in HTML?

While 'Retina display' is an Apple brand name, the term 'retina' is often used to describe high-DPI displays from other manufacturers. Likewise, retina is commonly used to refer to images optimized for high-DPI displays, which you'll learn about below.

What resolution should images be for Retina display?

To accommodate high resolution/retina displays, you'll want to use an image that's 1280 pixels wide, which is what's referred to as “2x”. While the graphic will still display on the website at 640 pixels wide, you are doubling the pixel density by using an image that's double the size.

What is retina-ready website?

Retina-ready websites are websites that utilise modern technology to display high-resolution images on devices that have high definition (HD) displays, such as the many tablets and smartphones, and some modern laptops, macbooks and desktop PCs.


2 Answers

Try adding #@2x.png at the end of your URL. That wont change the URL, but the image will be recognized as a retina @2x image. It worked for me, but I used this method with SDWebImage.

e.g. using http://www.msdomains.com/tmp/test.png#@2x.png.

like image 141
pre Avatar answered Sep 28 '22 09:09

pre


Your code should work pretty much as-is. I don't know what the original dimensions of your image were, but I'd guess they were 64x64 px. In order to scale down correctly, the original image would need to be 128x128 px.

As a test, the following code correctly displayed my photo in Retina resolution on the Simulator, and on my iPhone 4:

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.seenobjects.org/images/mediumlarge/2006-08-19-native-lilac.jpg"]]];

CGRect labelFrame = CGRectMake(0, 0, 375, 249.5);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:labelFrame];

[imageView setImage:img];
[self.view addSubview:imageView];

Note that the UIImageView is 375x249.5 points, which is half of the original (pixel) dimensions of the photo. Also, setting the contentScaleFactor didn't seem to be necessary.

(As an aside, I can't see that specifying @2x on the URL will help, in this case, as the call to dataWithContentsOfURL: will return an opaque blob of data, with no trace of the filename left. It's that opaque data that's then passed to imageWithData: to load the image.)

like image 42
Martin Kenny Avatar answered Sep 28 '22 07:09

Martin Kenny