Comparison of two picture compression methods when uploading pictures in IOS development

  • 2021-12-11 09:11:22
  • OfStack

Comparison of Two Image Compression Methods in IOS Image Upload

Incomplete idea of uploading pictures: Save the picture locally, Then upload the path of the picture to the server, and finally the server returns the path. This way is not scalable. If the user changes the mobile phone, there will be no picture path returned by the server in the sandbox of the new mobile phone, and the avatar that has been uploaded before cannot be obtained at this time, which is obviously infeasible in the project.

The correct way to upload pictures: Upload the avatar to the server 1 is to upload the picture NSData to the server, and the server returns a picture NSString address, and then change the path of NSString to url and request to update the user avatar through url (the user avatar is updated at this time by NSString)

The code is:


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 //  Format the request 
 manager.requestSerializer = [AFJSONRequestSerializer serializer];
 //  Set the return format 
 manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:[NSString stringWithFormat:@"%@%@", XLImageServerHost, functionName] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
///// Put the transmitted picture data here 
 NSData *eachImgData = UIImageJPEGRepresentation(image, 0.5);
 [formData appendPartWithFileData :eachImgData name : @"upload" fileName : @"picture.jpg" mimeType : @"image/jpeg" ];
 } success:^(AFHTTPRequestOperation *operation, id responseObject) {
 /// Request succeeded 
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 /// Request failed 
 }];

Now let's introduce 1: the difference between UIImageJPEGRepresntation and UIImagePNGRepresontation

There are two simple ways to read image data on Iphone: UIImageJPEGRepresentation and UIImagePNGRepresentation.

UIImageJPEGRepresntation:

The UIImageJPEGRepresentation method takes less time, while the UIImagePNGRepresentation method takes longer operation time


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

It may cause jamming when taking photos with UIImagePNGRepresentation

There are two simple ways to read image data on Iphone: UIImageJPEGRepresentation and UIImagePNGRepresentation.

UIImagePNGRepresontation:

The UIImageJPEGRepresentation function requires two parameters: the image reference and the compression factor, whereas the UIImagePNGRepresentation only requires the image reference as a parameter. In actual use, it is found that the image data returned by UIImagePNGRepresentation (UIImage* image) is much larger than that returned by UIImageJPEGRepresentation (UIImage* image, 1.0).

For example, when reading the photos of the same scenery taken by the camera, the data amount returned by UIImagePNGRepresentation () is 199K, while the data amount returned by UIImageJPEGRepresentation (UIImage* image, 1.0) is only 140KB, which is more than 50 KB less than the former. If the definition of the picture is not high, the second parameter of UIImageJPEGRepresentation function can be set to greatly reduce the amount of picture data.

For example, the picture just taken, By calling UIImageJPEGRepresentation (UIImage* image, 1.0) When reading data, The returned data size is 140KB, But after changing the compression factor, When reading data by calling UIImageJPEGRepresentation (UIImage* image, 0.5), the returned data size is only more than 11KB, which greatly compresses the amount of picture data, and the quality of picture is not obviously reduced from the perspective. Therefore, when reading the content of picture data, it is recommended to use UIImageJPEGRepresentation first, and according to your actual use scene, set the compression coefficient to further reduce the amount of picture data.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: