Retina display for image from url

I have some images that I need to get from the Internet. Just using the data from the URL.
They should display correctly on the Retina display.
When I get images from the Internet, they still look jagged. I need to set the image scale on the retina display (2.0), but I have to miss something. Here is what I have done 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]; 
+6
source share
4 answers

Your code should work as is. I do not know what the original sizes of your image were, but I would assume that they were 64x64 px. For proper scaling, the source image must 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]; 

Please note that UIImageView is 375x249.5 pixels, which is half the original (pixel) size of the photo. In addition, the installation of contentScaleFactor not required.

(Aside, I don’t see that specifying @2x in the URL will help, in this case, since calling dataWithContentsOfURL: will return an opaque data block, without a trace of the remaining file name. This is opaque data that was then transferred to imageWithData: for download Images.)

+3
source

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

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

+7
source

To display the retina, add the same image with a resolution that exactly matches the dual source image. Remember to add "@ 2x" at the end of this image name ... for example. "image_header.png" is a 320x100 image, then another image with the name " image_header@2x.png " (size 640x200) will be automatically selected to display the OS retina ... I hope this helps

0
source

when you directly assign the URL of an image in imageView, it will not perceive it as a retina.

 imageView.imageURL = [NSURL URLWithString:@"http://example.com/image.png"]; 

will not give you an image of the retina.

Thus, your image will be 200x200, but if your ViewView is 100x100, it will take 100x100 from the loaded image and display the pixelated image on the retina devices.

The solution should be to use the imageView image property instead of imageURL.

 imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/image.png"]]]; 

This will assign a 100x100 image to the 200x200 image and therefore the image will not be pixelated.

0
source

Source: https://habr.com/ru/post/920517/


All Articles