简体   繁体   English

从可调整大小的UIImageView的图像中裁剪部分

[英]crop section from resizeable UIImageView's image

I have a UIImageView that gets its image from UIImagePickerController The UIImageView is set to Aspect Fit Im using gesture recognizers to move, scale, and rotate this UIImageView 我有一个UIImageView,它从UIImagePickerController获取图像。UIImageView设置为Aspect Fit Im,使用手势识别器来移动,缩放和旋转此UIImageView

I have a second, much smaller UIImageView The second UIImageView is in front of and centered with the first 我有第二个,小得多的UIImageView第二个UIImageView在第一个的前面并居中

What I would like to do is be able to move, scale, and rotate the first UIImageView into position, then press a button to create a new image containing only the section of the original image that can be seen within the bounds of the second UIImageView 我想做的是能够移动,缩放和旋转第一个UIImageView到位,然后按一个按钮以创建一个仅包含原始图像在第二个UIImageView范围内可见的部分的新图像。

I have tried quite a few ways of cropping the image without success. 我尝试了很多方法来裁剪图像而没有成功。 Everything I tried will crop the image but the returned image is not what I expect. 我尝试的所有操作都会裁剪图像,但是返回的图像不是我期望的。 What I get back appears to be a rotated portion of the top, right hand, section of the FULL SIZE image. 我得到的结果似乎是“全尺寸”图像顶部,右侧部分的旋转部分。

I am using the UIImage Categories from Trevor's Bike Shed 我正在使用Trevor的Bike Shed中的UIImage类别

My code: 我的代码:

.h 。H

@interface BubbleBossViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIGestureRecognizerDelegate>{
    IBOutlet UIImageView *smallCropperWindow;
    IBOutlet UIImageView *imageViewForChosenImage;
    IBOutlet UIView *canvas;
    CGFloat _lastScale;
    CGFloat _lastRotation;
    CGFloat _firstX;
    CGFloat _firstY;
    CAShapeLayer *_marque;

}
@property BOOL newMedia;
- (IBAction)useCamera:(id)sender;
- (IBAction)useCameraRoll:(id)sender;
- (IBAction)crop:(id)sender;

@end

relevant .m -(void)crop:(id)sender{ 相关.m- (void)crop:(id)sender {

smallCropperWindow.image=[imageViewForChosenImage.image croppedImage: (CGRect)smallCropperWindow.frame];

} }

in UIImage+Resize.h 在UIImage + Resize.h中

- (UIImage *)croppedImage:(CGRect)bounds;

in UIImage+Resize.m 在UIImage + Resize.m中

- (UIImage *)croppedImage:(CGRect)bounds {
    CGFloat scale = MAX(self.scale, 1.0f);
    CGRect scaledBounds = CGRectMake(bounds.origin.x * scale, bounds.origin.y * scale, bounds.size.width * scale, bounds.size.height * scale);
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], scaledBounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:UIImageOrientationUp];
    CGImageRelease(imageRef);
    return croppedImage;
}

Screenshot showing issue: 屏幕截图显示了问题: 在此处输入图片说明

The small square in the middle should be filled with the babbie's face not a section of the floor from the full size image. 中间的小方块应充满婴儿的脸,而不是全尺寸图片的地板一部分。

What am I doing wrong here? 我在这里做错了什么?

Calculations you are doing seem incorrect. 您正在进行的计算似乎不正确。 Try this, 尝试这个,

  1. First you have to make image aspect fit. 首先,您必须使图像外观合适。
  2. Then find the ratio heightRatio=image.size.height/imageView.frame.height; 然后找到比率heightRatio=image.size.height/imageView.frame.height;
  3. Then find the ratio weidthRatio=image.size.width/imageView.frame.width; 然后找到比率weidthRatio=image.size.width/imageView.frame.width;

Now instead of passing your smallCropperWindow.frame you have to do three things, 现在,不必传递smallCropperWindow.frame ,而要做三件事,

  1. Translate center of small croppedWindow.Lots of calculation will go because aspect fit image will not start from origin of imageview. 平移小作物窗口的中心。将进行大量计算,因为宽高比适合的图像不会从imageview的原点开始。
  2. Scale the width and height of your croppedWindow and pass this frame for cropping. 缩放croppedWindow的宽度和高度,然后传递此帧进行裁剪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM