2

I get image for imageView using UIImagePickerController like this

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *originalImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    CGSize destinationSize1 = CGSizeMake(200, 200);
    UIGraphicsBeginImageContext(destinationSize1);
    [originalImage drawInRect:CGRectMake(0,0,destinationSize1.width,destinationSize1.height)];
    UIImage *newImage1 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    firstimage1.image = newImage1;
    [picker dismissViewControllerAnimated:YES completion:nil];

    NSLog(@"image width is %f",firstimage1.image.size.width);
    NSLog(@"image height is %f",firstimage1.image.size.height);
   if (firstimage1.image.size.width==200 && firstimage1.image.size.height==200) {
    UIAlertView *alertcrp=[[UIAlertView alloc]initWithTitle:@"message!" message:@"you want to crop this image" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
       [alertcrp addButtonWithTitle:@"Crop "];
        [alertcrp show];
        }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"Button Index =%ld",(long)buttonIndex);
    if (buttonIndex == 0)
    {
        NSLog(@"You have clicked Cancel");
    }
    else if(buttonIndex == 1)
    {
   UIImagePickerController *controller = [[UIImagePickerController alloc] init];

        controller.allowsEditing = YES; // this will allow you to crop the image first
        controller.delegate = self;

        [self presentViewController:controller animated:YES completion:nil];

    }
}

We resize image to 200*200.Now What i need is When User is upload image .If image is upload success and then open alertView "if you want to Crop the image". Once click the crop button i need to Open Rectangle crop View .

Please give me any idea about my problem.Thanks in Advanced.

3 Answers3

1

It is pretty simple what you need to do. First create a UIScrollView of the crop size you want to crop, u can make it dynamic if you want to by pan gestures etc. But I have done with a constant crop size so I wont go into that detail. Next create an UIImageView of the size of your original image and set its image proprety to that. Also set your scorllview's content size property to the exact same as the image's size. Allow the bounces to have some extra effects. Then on press of crop you just need to pass the scrollview's contentoffset and size you want to be croped(that would be size of scrollview i.e. its width and height) to this function.

- (UIImage *)crop:(UIImage *)image inRect:(CGRect)cropRect
{
    CGImageRef cropRef = CGImageCreateWithImageInRect(image.CGImage, cropRect);
    UIImage* cropImage = [UIImage imageWithCGImage:cropRef];
    CGImageRelease(cropRef);

    return cropImage;
}

You can call this method as per above description as

UIImage * img = [[CommonFunctions sharedManager] crop:imgView.image inRect:CGRectMake(scrollView.contentOffset.x , scrollView.contentOffset.y , reqWidth, reqHeight)];

There might be a bug with making the rect for cropping according to your logic. But you should have general idea on what do. Do let me know if something confuses you or if it helps or not, so I might be able to help out further :)

Abdul91
  • 708
  • 5
  • 16
0

For resize an image use this

UIGraphicsBeginImageContext(CGSizeMake(200, 200));
[myImage drawInRect:CGRectMake(0, 0, 200, 200)]; // Size of your ne image
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
0
UIImagePickerController *controller = [[UIImagePickerController alloc] init];

controller.allowsEditing = YES; // this will allow you to crop the image first
controller.delegate = self;

[self presentViewController:controller animated:YES completion:nil];

Then you will need to resize it to fit 200x200, you could try this approach https://stackoverflow.com/a/537697/660907

Community
  • 1
  • 1
marvin_yorke
  • 3,469
  • 4
  • 25
  • 35