Mohanraj S K
Mohanraj S K

Reputation: 657

iOS imageview crop not working when work with device photos

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


}

Upvotes: 1

Views: 1016

Answers (3)

norders
norders

Reputation: 1252

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

Upvotes: 1

mert
mert

Reputation: 1098

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

Upvotes: 0

Vladimir K
Vladimir K

Reputation: 1382

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

Upvotes: 1

Related Questions