我计划创建一个图像编辑应用程序。第一步,我要将图像的触摸位置显示到与原始 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。
这里有几个关于如何解决这个问题的答案:
关于处理设备照片时 iOS ImageView 裁剪不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643831/