Image resolution and pixel alignment in iOS development, and pixel alignment in ios resolution
Concept of pixel alignment
In iOS, there is a concept called pixel alignment. If the pixels are not alignment, interpolation is required during GPU rendering. This interpolation process may cause performance loss.
On the simulator, there is an option to display the non-alignment of pixels.
Logical and physical pixels
On iOS devices, there are the concept of point (logical pixel) and pixel (physical pixel.
During programming, point is used, and pixel is used for actual rendering. A point can correspond to multiple pixel entries.
The ratio of point to pixel is[[UIScreen mainScreen] scale]
.
UIImage scale Concept
If you load an image from a file whose name includes the @2x modifier, the scale is set to 2.0. You can also specify an explicit scale factor when initializing an image from a Core Graphics image. All other images are assumed to have a scale factor of 1.0.
Image also has the concept of size.
This value reflects the logical size of the image and takes the image’s current orientation into account. Multiply the size values by the value in the scale property to get the pixel dimensions of the image.
That is to say, the size of the image is multiplied by the size of the image and the scale to obtain the size of the physical pixel.
Problem
If pixels are not aligned, what is the physical pixel (pixel) and logical pixel (point) alignment?
Lab
Use a png Image of 300*225 pixels. Load the data to the memory using different methods to get different size and scale, and then store the data in the imageview of different size. Usecolor misaligned images
To determine whether the pixels are aligned.
Here the simulator uses the iPhone 6 and the screen scale is 2.
Image size |
Image scale |
Imageview Size |
Misalign? |
300,225 |
1 |
300,225 |
Yes |
300,225 |
1 |
150,112.5 |
No |
150,112.5 |
2 |
150,112.5 |
No |
150,112.5 |
2 |
75, 56.25 |
Yes |
Some code
NSLog(@"screen scale is %f",[[UIScreen mainScreen] scale]); UIImage *image = [UIImage imageNamed:@"test.png"]; NSLog(@"image size %@, scale %f ", [NSValue valueWithCGSize:image.size], image.scale); UIImageView *imageView = [[UIImageView alloc] initWithImage:image];// imageView.frame = CGRectMake(50, 100, imageView.bounds.size.width * 2/3, imageView.bounds.size.height * 2/3); imageView.frame = CGRectMake(50, 100, imageView.bounds.size.width, imageView.bounds.size.height); NSLog(@"imageView frame %@", [NSValue valueWithCGRect:imageView.frame]); [self.view addSubview:imageView];
Conclusion