使用设备照片时,iOS imageview裁剪不起作用

时间:2015-04-15 07:14:40

标签: ios objective-c uiimage crop xcode6.1

我计划创建一个图像编辑应用程序。第一步我要将触摸的图像位置显示在原始图像视图的单独图像视图中。

使用默认图片(其中一个是从xcode storyboard属性检查器设置)进行测试时,它的工作正常。

但当我从设备导入照片"照片" 时,它不能拍出精确的图像。

我真的很困惑并坚持到那里。请一些人指导我完成这项任务。

我已尝试使用以下代码

提前致谢。

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


}

3 个答案:

答案 0 :(得分:1)

看起来您正在从视图传递坐标以裁剪为图像。图像视图及其图像很少具有相同的尺寸,尤其是当您从照片中选取图像时。

尝试在发送要裁剪的图像之前将第一个视图渲染到图像中。您可以通过向UIView添加类别来执行此操作:

@implementation UIView (Image)

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

@end

编辑:或者,如果您只想让它在没有类别的情况下工作,请将此方法添加到您的代码中:

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

然后将现有代码修改为:

croperImgvw.image = [self cropImage:[self imageOfView:imgVw] :point];

答案 1 :(得分:1)

据我所知,您传递给 cropImage:image:方法的 point 参数来自UIImageView坐标系 - 并且CGImageCreateWithImageInRect中的rect参数必须取自UIImage ,而不是来自UIImageView。

以下是如何解决此问题的几个答案:

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

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

答案 2 :(得分:0)

除了诺德的回答由于分辨率不同,添加比例参数也很好。

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