To compress UIImage,it depends on you demand.According to the quality of compressed or According to the sizeThe compression.
The first way(quality),use API as follow:
UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality)
The second way,use code as follow:
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
To upload you local image file to server,if you use AFNetWorking 3.x,such code may avaiable to you:
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:host]];
[manager POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//local file url,for you is /Users/macmini/Library/Developer/CoreSimulator/Devices/89104EAC-5BE1-4BEC-BE8E-7B8FFBF9CBD2/data/Media/DCIM/100APPLE/IMG_0005.JPG
NSURL *url = [NSURL fileURLWithPath:fileUrl];
[formData appendPartWithFileURL:url name:name fileName:fileName mimeType:mimeType error:nil];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (successBlock) {
if (responseObject) {
successBlock(responseObject);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failureBlock) {
HttpFailResponse *failResponse = [self _kep_configureFailResponseWithError:error];
failureBlock(failResponse);
}
}];