This is the 28th day of my participation in the August Gwen Challenge. For details: August Gwen Challenge “juejin.cn/post/698796…”
preface
How to compress: Find recurring strings and shorten them by replacing them with shorter symbols.
For example, if an article uses the word “# publicid: : iOS reverse “extensively, we shorten it by 6 characters by replacing it with “iOS”, and 8 characters by replacing it with” I “.
In fact, repeated strings can be replaced with any character as long as correspondence is maintained.
Essentially, “compressing” means finding a probability distribution for the contents of a file and replacing those parts with shorter ones that are more likely to appear.
So, the more repetitive the file, the smaller it can be compressed.
For example, “ABABABABABABAB” can be compressed to “7AB”.
I Image compression in two ways
- Quality compression: that is, the loss of picture quality at the cost of size (length and width size) unchanged;
UIImageJPEGRepresentation (image, 0.0); //JPEG NSData * imageData = UIImagePNGRepresentation(image); '// PNG is recommended because' PNG: 0x89 image/ PNG, less compression than JPG, but lossless compression, high decompression performance, Apple recommended image format!Copy the code
- Pixel compression (proportional compression) : By reducing the number of pixels in the width and length direction;
[sourceImage drawInRect: CGRectMake (0, 0, targetWidth, targetHeight)];Copy the code
This paper’s compression scheme: mass compression (0.5) combined with proportional compression (1028) :
1, quality compression ratio (quality compression using system APIUIImageJPEGRepresentation) 2, percentage compression using: drawInRect
II. Solve the problem of blurred pictures after compression
Most of the reason for the blur problem: using third-party frameworks (like QMUIKit), when selecting album previews, the image is compressed and blurred.
Application scenario: For the protocol type
2.1. If you take photos, you can compress the resolution, otherwise uploading will be slow
Do not upload with preview images
// [self imgUpLoad:imageAsset.previewImage Max:1 Index:1];
[self imgUpLoad:imageAsset.originImage Max:1 Index:1];
Copy the code
If you are taking photos, you can compress the resolution, otherwise the upload will be slow
return [self imageCompressForSize:image targetSize:size];
Copy the code
Photo compression resolution example
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; // If ([type isEqualToString:@"public. Image "]) {UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage]; image1 = [ControlManager image:image1 scaleToSize:CGSizeMake(1000, 1200)]; / / compression resolution Easy to upload [picker dismissViewControllerAnimated: YES completion: nil]; [SVProgressHUD showWithStatus: @ "on the cross.." maskType: SVProgressHUDMaskTypeGradient]; [self imgUpLoad:image1 Max:1 Index:1]; }}Copy the code
2.2 Mass compression (0.5) combined with proportional compression (1028)
usage
NSData * imageData = UIImageJPEGRepresentation( [image compressWithTargetPixel:1028], k_UIImageJPEGRepresentationcompressionQuality);
Copy the code
The ratio of mass compression
(quality compression using APIUIImageJPEGRepresentation) system
# define k_UIImageJPEGRepresentationcompressionQuality 0.5Copy the code
Proportional compression code
- (UIImage *)compressWithTargetPixel:(NSUInteger)targetPx {Copy the code
III Select an image format based on the service scenario
3.1 How to distinguish images of different formats
- Judging by the first byte of image data!
// NSData+ImageContentType.m + (NSString *)sd_contentTypeForImageData: PNG: 0x89 image/ PNG, less compression than JPG, but lossless compression, high decompression performance, Apple recommended image format! JPG: 0xFF image/ JPEG, the highest compression ratio of a picture format, lossy compression! Most used scene, camera! Decompression performance is not good! GIF: 0x47 image/ GIF, sequence frame GIF, features: only support 256 colors! The most popular time is 1998 ~ 1999, have patent!Copy the code
3.2 Application Scenario Analysis
3.2.1 JPEG/JPG
- Features: lossy compression, small size, does not support transparency
- Use scenarios: For colorful photos, JPG is a universal choice, such as a large background, rotation, or Banner.
3.2.2 PNG
- Features: lossless compression, high quality, large volume, transparent support.
- Scenarios: Business scenarios with transparent images, such as small logos, simple and contrasting images or backgrounds.
3.2.3 SVG
- Features: text files, small size, no distortion, good compatibility, no need for multi-terminal, multi-resolution adaptation, strong interactive features, high rendering cost.
- Use scenario: the picture color is relatively simple business scene.
3.2.4 Base64
- Features: text file, dependent encoding, Base64 encoding, image size will swell to 4/3 of the original file.
- Usage scenario: Small icon (<8KB) solution.
3.2.5 WebP
- Features: WebP images is a new image format developed by Google. Compared with PNG and JPG, the size of WebP image is reduced by about 30% under the same visual experience. In addition, WebP image formats also support lossy compression, lossless compression, transparency, and animation, but there are compatibility issues.
- Usage scenario: shopping mall websites with large images are currently used by Google, Facebook, ebay, Taobao, Tencent, Meituan and so on.
“If you open Taobao.com, if you are a Chrome browser, you will see that all the images end in WebP, and Taobao.com images use WebP. If you are Safari browser, see the picture is JPG or PNG, Taobao automatically judge browser support webP, if support, output the corresponding picture format!
Insert a picture description here
Img.alicdn.com/imgextra/i4…
IV, see also
For more content, please check the original article or follow the public account: iOS Reverse
Kunnan.blog.csdn.net/article/det…