1

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


}
Mohanraj S K
  • 657
  • 4
  • 16

3 Answers3

1

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:

@implementation UIView (Image)

- (UIImage *)image {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

Edit: Or if you just want to get it working without categories, add this method to your code:

- (UIImage *)imageOfView:(UIImageView *)imageView {
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);
    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Then modify your existing code to read:

croperImgvw.image = [self cropImage:[self imageOfView:imgVw] :point];
norders
  • 1,160
  • 9
  • 13
  • thanks for reply, I am new to iOS Development and can you please help me to where or how i am add your code or idea. – Mohanraj S K Apr 15 '15 at 07:56
  • I've edited my answer so you can just add the code in-line. You also don't need to worry about scaling for different screen sizes as the 0.0 parameter in the UIGraphicsBeginImageContextWithOptions call automatically selects the correct scale factor for the device's screen. – norders Apr 15 '15 at 08:31
  • I have try those code and it's also assign a nearest one fromthe source image, for an example if i am click near "eye" from image means its crop a image near "Neck" or "Mouth". i have working to solve this issue from morning still can't get solution. – Mohanraj S K Apr 15 '15 at 09:28
  • 1
    Oh, something else I just spotted - you probably want the line `CGPoint point = [touch locationInView:self.view];` to read `CGPoint point = [touch locationInView:imgVw.view];` instead. – norders Apr 15 '15 at 10:25
  • @Mohanraj, did any of these answers work for you? If so please select one as the correct answer. (p.s. I'm adding this to my own answer because this is a new account and I don't yet have enough rep to comment on the question). – norders Apr 17 '15 at 07:59
  • thanks for your help and i get liitle near solution with your code and Vladimir's link solution,. i have combined both of your code after i get that. One again thank you so much. – Mohanraj S K Apr 17 '15 at 10:35
1

As I understand, your point parameter that you are passing to cropImage:image: method are from UIImageView coordinate system - and rect parameter in CGImageCreateWithImageInRect must be taken from UIImage, not from UIImageView.

Here is couple answers of how you can solve this problem:

https://stackoverflow.com/a/10865552/4495995

https://stackoverflow.com/a/21693491/4495995

Community
  • 1
  • 1
Vladimir K
  • 1,382
  • 1
  • 12
  • 27
0

Addition to norder's answer It's good to add scale parameter because of the different resolutions.

TakeSnapshot.h

#import <Foundation/Foundation.h>

@interface TakeSnapshot : NSObject


+(UIImage *)takeSnapshotFromScreenWithSize:(UIView *)view Area:(CGPoint)screenPoint; 

@end

TakeSnapshot.m

#import "TakeSnapshot.h"

@implementation TakeSnapshot
+(UIImage *)takeSnapshotFromScreenWithSize:(UIView *)view Area:(CGPoint)screenPoint{
    {
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
            UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
        }
        else
            UIGraphicsBeginImageContext(view.bounds.size);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        //    
        if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])  UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.width),NO,[UIScreen mainScreen].scale);
        else
            UIGraphicsBeginImageContext(view.bounds.size);
        [screenImage drawAtPoint:screenPoint];
        UIImage *shareImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return shareImage;

    }

}
@end
mert
  • 1,090
  • 13
  • 26