I have planed to create a image edit application. first step i gonna show touched position of image in to a separate image view from original image view.
Its's working fine when test with default image(which one is set from xcode storyboard attribute inspector).
But its not crop a exact image when i import a photo from device "Photos".
I am really confused and stuck on there. please some one guide me to do this task.
I have try with the Below code
Thanks in advance.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
imgVw.image = image;
// croperImgvw.image = [self cropImage : image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (UIImage *)cropImage:(UIImage *)image : (CGPoint)point
{
CGRect clippedRect =CGRectMake(point.x, point.y, 50, 50);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touch_point = [touch locationInView:self.view];
NSLog(@"X location: %f", touch_point.x);
NSLog(@"Y Location: %f",touch_point.y);
CGPoint point = [touch locationInView:self.view];
croperImgvw.image = [self cropImage:[imgVw image] :point];
}
It looks like you're passing a coordinate from a view in order to crop to an image. An image view and its image will rarely have the same dimensions, especially if you're picking images from Photos.
Try rendering the first view into an image before sending that image to be cropped. You can do this by adding a category to UIView like this:
Edit: Or if you just want to get it working without categories, add this method to your code:
Then modify your existing code to read: