处理设备照片时 iOS ImageView 裁剪不起作用

标签 ios objective-c uiimage crop xcode6.1

我计划创建一个图像编辑应用程序。第一步,我要将图像的触摸位置显示到与原始 ImageView 不同的 ImageView 中。

在使用默认图像(从 xcode storyboard attribute inspector 设置)进行测试时,它工作正常。

但是当我从设备“照片”导入照片时,它不会裁剪出精确的图像。

我真的很困惑,一直卡在那里。请有人指导我完成这项任务。

我试过下面的代码

提前致谢。

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


}

最佳答案

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

这里有几个关于如何解决这个问题的答案:

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

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

关于处理设备照片时 iOS ImageView 裁剪不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643831/

相关文章:

c++ - 设置 iOS 项目以使用 eigen

ios - Swift - 在 CGPoint 比较颜色

ios - 如何调整图像大小以适应 tableview 单元格中的 UIImageview

ios - 将 NSMutableAttributedString 添加到 TextField

ios - 选择视频后,UIImagePickerController 在 iOS10 模拟器上卡住压缩视频

ios - iOS-如何使录制的音频文件变小(就像微信一样)?

iOS UItableviewCell 清除旧的单元格值

ios - 如何在不重置其当前状态的情况下从一个 viewcontroller 到自身的 segue?

ios - 检测 UILabel 文本的变化

swift - 如何将 Xcode Assets 文件夹中的图像数组添加到 ViewController?