-1

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

}
Paul
  • 1,179
  • 3
  • 14
  • 38
  • 1
    What's `WUTModelImageUploadReq`, especially it's property `photo`? What's `encodeToBase64String:`? – Larme Jul 28 '15 at 16:18
  • where are you cropping the image here? – Teja Nandamuri Jul 28 '15 at 16:29
  • I'm cropping it here `UIImage *newImage = [self resizeImage:self.image toWidth:320.0f andHeight:480.0f];` – Paul Jul 28 '15 at 16:54
  • @Larme Photo is a the property that hold the image. – Paul Jul 28 '15 at 16:55
  • It's still unclear. photo is a UIImage? a NSString? What's the definition of `encodeToBase64String:`? – Larme Jul 29 '15 at 08:47
  • @Larme photo has a property and is a NSString. This is the helper method for encodeToBase64String: `- (NSString *)encodeToBase64String:(UIImage *)image { return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; }` – Paul Jul 29 '15 at 15:59
  • In the line with issue replace fileData with newImage. – Larme Jul 29 '15 at 16:04
  • @Larme Sorry, but Xcode throws an error. Error is: Use of undeclared identifier 'newImage' – Paul Jul 29 '15 at 16:11
  • imageUploadReq.photo = [self encodeToBase64String:[UIImage imageWithData:fileData]]; – Larme Jul 29 '15 at 16:12
  • @Larme It didn't work. The photo didn't upload to the server in the app. – Paul Jul 29 '15 at 16:23
  • What shows NSLog(@"Error: %@", error") if that fails? – Larme Jul 29 '15 at 16:27
  • @Larme I tried putting in the `NSLog(@"Error: %@", error);` but Xcode didn't spit anything out in the logs when it failed. – Paul Jul 29 '15 at 17:37

1 Answers1

0

I think, you need this:

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

return newImage;    }

You just need to pass original image and your desired size and it will return your expected image.

Call the method like:

UIImage *originalImage = [info valueForKey:UIImagePickerControllerOriginalImage]; // here originalImage is image taken from camera or you can use whatever you want

UIImage * image = [GlobalFunction imageWithImage:originalImage scaledToSize:CGSizeMake(200, 200)]; // GlobalFunction is class where I have defined this method and it returns UIImage of size (200, 200)
Bhanupriya
  • 1,202
  • 1
  • 9
  • 20
  • Thanks. Is this a category or a helper method? – Paul Jul 29 '15 at 16:01
  • Its a helper method. Just pass your original image and expected size and it will return cropped image with your expected size. If this is what you needed, then accept the answer. – Bhanupriya Jul 30 '15 at 04:35
  • How do I pass an image using this helper method? Like this? https://gist.github.com/lumierephoto/04664b705b80b63d8cb4 – Paul Jul 30 '15 at 05:12