Upload pictures not comprehensive idea: Save the picture to the local, and then the path to the image to the server, and finally by the server to return the path, this way is not extensible, if the user changed the phone, then the new cell phone's sandbox there is no server return image path, at this time can not get the picture has been uploaded , which is clearly not available in the project.
Upload pictures The correct way: upload avatar to the server is generally the picture NSData uploaded to the server, the server returned a picture nsstring address, Then the nsstring path to the URL and through the URL request to update the user picture (the user picture is now updated is NSString)
The code is :
Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager Manager]; //Set Request FormatManager.requestserializer =[Afjsonrequestserializer Serializer]; //Set return formatManager.responseserializer =[Afjsonresponseserializer Serializer]; [Manager post:[nsstring stringWithFormat:@"%@%@", Xlimageserverhost, functionname] Parameters:nil constructingbodywithblock:^ (ID<AFMultipartFormData>formData) {/////Pass the picture data put hereNSData *eachimgdata = uiimagejpegrepresentation (image,0.5); [FormData appendpartwithfiledata:eachimgdata Name:@"Upload"FileName:@"picture.jpg"MimeType:@"Image/jpeg" ]; } Success:^ (Afhttprequestoperation *operation,IDresponseobject) { ///Request succeeded} failure:^ (Afhttprequestoperation *operation, Nserror *error) { ///request failed}];
Now let's introduce: the difference between uiimagejpegrepresntation and uiimagepngrepresontation
There are two simple ways to read image data on the iphone: UIImageJPEGRepresentation and Uiimagepngrepresentation.
Uiimagejpegrepresntation:
UIImageJPEGRepresentation methods are less time consuming and uiimagepngrepresentation take longer to operate
-(void) Imagepickercontroller: (uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: ( Nsdictionary *) Info
May cause lag when using uiimagepngrepresentation to get photos
There are two simple ways to read image data on the iphone: UIImageJPEGRepresentation and Uiimagepngrepresentation.
Uiimagepngrepresontation:
The UIImageJPEGRepresentation function requires two parameters: a reference to the picture and a compression factor. Instead, uiimagepngrepresentation only requires a picture reference as a parameter. Through the actual use of the process, the comparison found: Uiimagepngrepresentation (uiimage* image) is much larger than the amount of image data returned by uiimagejpegrepresentation (uiimage* image, 1.0).
For example, the same picture that reads the same view taken by the camera, Uiimagepngrepresentation () returns a size of 199K, while UIImageJPEGRepresentation (uiimage* image, 1.0) The amount of data returned is only 140KB, which is less than 50 KB. If the clarity of the picture is not required, you can also reduce the amount of image data by setting the second parameter of the UIImageJPEGRepresentation function.
For example, a picture just taken, when reading data by calling UIImageJPEGRepresentation (uiimage* image, 1.0), returns the data size of 140KB, but after changing the compression factor, by calling UIImageJPEGRepresentation (uiimage* image, 0.5) to read the data, the returned data size is only more than 11KB, greatly compressing the amount of data in the image, and from the perspective point of view, the quality of the picture is not significantly reduced. Therefore, When reading the image data content, it is advisable to use uiimagejpegrepresentation, and according to their actual use of the scene, set the compression coefficient, further reduce the size of the image data.
IOS: Comparison of two image compression modes when uploading images