我试图在使用UIImageJPEGRepresentation上传到服务器之前压缩图像。
到目前为止,我已经将图像更改为PNG,并将质量更改为0.01f。我试过几种不同的写法。在发送到服务器之前这是最优的吗?我应该改变什么来优化?
- (void)uploadPhoto {
WUTModelImageUploadReq *imageUploadReq = [[WUTModelImageUploadReq alloc]init];
imageUploadReq.photo = [self encodeToBase64String:[UIImage imageWithData:UIImageJPEGRepresentation(self.viewControllerPost.imageForPost, 0.01f)]];
imageUploadReq.extension = @"png";
void (^wsSuccessHandler)(AFHTTPRequestOperation *operation, NSDictionary* responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"Pull Feed responseObject %@",responseObject);
NSError *error;
WUTModelPostImageResponse *wsResponse = [[WUTModelPostImageResponse alloc]initWithDictionary:(NSDictionary *)responseObject error:&error];
if (error) {
errorMessage = @"Failure to upload image.";
[self postExecuteFail];
}
else{
if (wsResponse.success) {
WUTModelImage *imageTemp = [wsResponse.data firstObject];
[postItem setObject:imageTemp.photo forKey:@"photo"];
[self uploadPostFeed];
}else{
errorMessage = @"Failure to upload image.";
[self postExecuteFail];
}
}
};
建议:
- 使用扩展名"jpg",而不是"png"。
- 使用0.0的compressionQuality来最大化压缩。
- 考虑调整图像的大小,如果你的情况允许。