Cropping an image before sending to the server on iOS

时间:2015-07-28 16:05:02

标签: ios objective-c uiimage uiimagejpegrepresentation

I'm trying to crop an image before sending to the server and I'm having issues.

I'm was trying to do this:

imageUploadReq.photo = [self encodeToBase64String:[UIImage imageWithData:UIImageJPEGRepresentation(fileData, 0.07f)]];

But Xcode is complaining that "Incompatible pointer types passing NSData * to parameter type UIImage". I tried to cast it, but it wouldn't work either.

Here is the code:

- (void)uploadPhoto {

   NSData *fileData;

   if (self.image != nil) {
       UIImage *newImage = [self resizeImage:self.image toWidth:320.0f andHeight:480.0f];
       fileData = UIImageJPEGRepresentation(newImage, 0.07f);
   }

    WUTModelImageUploadReq *imageUploadReq = [[WUTModelImageUploadReq alloc]init];

    // I'm trying to set the first parameter of UIImageJPEGRepresentation to fileData 
    imageUploadReq.photo = [self encodeToBase64String:[UIImage imageWithData:UIImageJPEGRepresentation(self.viewControllerPost.imageForPost, 0.07f)]];
    imageUploadReq.extension = @"jpg";


    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];
        }
     }
  };

    void (^wsErrorHandler)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error){
    if ([error.localizedDescription rangeOfString:@"401"].location != NSNotFound)
        errorMessage = @"It seems that your login session get expire, Please relogin after logged out.";
    else
        errorMessage = @"Failure to upload image.";
    [self postExecuteFail];
  };

  AFHTTPRequestOperation *op = [WUTCommonWebServices WebServicePostCallWithAccessTokenForEndPoint:WS_UploadImage WithJson:imageUploadReq ForSuccess:wsSuccessHandler ForFailure:wsErrorHandler];
  [op start];

}

1 个答案:

答案 0 :(得分:0)

我想,你需要这个:

SELECT DISTINCT --[INSERT NEEDED COLUMNS HERE]
FROM
    tbl_user 
JOIN 
    tbl_assign_role ON tbl_user.u_id = tbl_assign_role.tar_owner_id 
WHERE   
    is_active = 1 
    AND u_id != 1 
    AND tar_is_deleted = 0 
ORDER BY 
    tbl_user.u_updated_date DESC

您只需传递原始图像和所需尺寸,它就会返回您想要的图像。

调用方法如:

+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;    }