Ios 将UIScrollViewPoint转换为UIImage+;缩放+;旋转
我需要用另一个Ios 将UIScrollViewPoint转换为UIImage+;缩放+;旋转,ios,objective-c,uiscrollview,uiimageview,uigraphicscontext,Ios,Objective C,Uiscrollview,Uiimageview,Uigraphicscontext,我需要用另一个UIView的某个矩形裁剪UIImage加载到uicrollview中的UIImage,该矩形也在uicrollview 下面是视图层次结构 --> View --> UIScrollView --> viewBase (UIView) --> UIImageView -> (Zoomed & rotated ) --> UIView (Target Vie
UIView
的某个矩形裁剪UIImage
加载到uicrollview
中的UIImage
,该矩形也在uicrollview
下面是视图层次结构
--> View
--> UIScrollView
--> viewBase (UIView)
--> UIImageView -> (Zoomed & rotated )
--> UIView (Target View)(Movable User can move anywhere in scrollview to crop rect)
我的图像被旋转和缩放,我需要在TargetView
我正在绘制UIImage
,在context
上旋转,下面是代码
CGFloat angleCroppedImageRetreacted = atan2f(self.imgVPhoto.transform.b, self.imgVPhoto.transform.a);
angleCroppedImageRetreacted = angleCroppedImageRetreacted * (180 / M_PI);
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.imgVPhoto.image.size.width, self.imgVPhoto.image.size.height)];
rotatedViewBox.transform = CGAffineTransformMakeRotation(-angleCroppedImageRetreacted);
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0f, rotatedSize.height / 2.0f);
CGContextRotateCTM(bitmap, -angleCroppedImageRetreacted);
CGContextScaleCTM(bitmap, 1.0f, -1.0f);
CGContextDrawImage(bitmap, CGRectMake(-self.imgVPhoto.image.size.width / 2.0f,
-self.imgVPhoto.image.size.height / 2.0f,
self.imgVPhoto.image.size.width,
self.imgVPhoto.image.size.height),
self.imgVPhoto.image.CGImage);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
而且效果很好。我得到的图像和我在模拟器中看到的一样
对于将目标点视图转换为UIImage
我使用以下不起作用的代码
CGPoint imageViewPoint = [self.viewBase convertPoint:self.targetImageview.center toView:self.imgVPhoto];
float percentX = imageViewPoint.x / self.imgVPhoto.frame.size.width;
float percentY = imageViewPoint.y / self.imgVPhoto.frame.size.height;
CGPoint imagePoint = CGPointMake(resultImage.size.width * percentX, resultImage.size.height * percentY);
rect.origin = imagePoint;
//rect.origin.x *= (self.imgVPhoto.image.size.width / self.imgVPhoto.frame.size.width);
//rect.origin.y *= (self.imgVPhoto.image.size.height / self.imgVPhoto.frame.size.height);
imageRef = CGImageCreateWithImageInRect([resultImage CGImage], rect);
img = [UIImage imageWithCGImage:imageRef scale:viewImage.scale orientation:viewImage.imageOrientation];
我认为问题是我们不能在转换应用后使用Rect
请帮助我裁剪UIImage
,它在同一层次上从rect缩放和旋转
如果您需要更多信息,请询问我自己的问题 感谢Matic给了我一个想法 我改变了逻辑 我已经实现了我想要的功能
CGPoint locationInImageView = [self.viewBase convertPoint:self.targetImageview.center toView:self.view]; // received from touch
locationInImageView = [self.view convertPoint:locationInImageView toView:self.imgVPhoto];
// I GOT LOCATION IN UIIMAGEVIEW OF TOUCH POINT
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);
[self.imgVPhoto.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// I GOT UIIMAGE FROM CURRENT CONTEXT
CGFloat width = self.targetImageview.frame.size.width * self.zoomScale ;
CGFloat height = self.targetImageview.frame.size.height * self.zoomScale ;
//2 IS SCALE FACTOR
CGFloat xPos = (locationInImageView.x * 2) - width / 2;
CGFloat yPos = (locationInImageView.y * 2) - height / 2;
CGRect rect1 = CGRectMake(xPos, yPos, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([img1 CGImage], rect1);
// YAHHH YOU HAVE EXACT IMAGE
UIImage *img = [UIImage imageWithCGImage:imageRef scale:img1.scale orientation:img1.imageOrientation];
目标视图和图像视图之间的关系是什么?如果UIImage有方向,我怀疑旋转代码是否有效(试着在运行时拍摄一张肖像照片,并从几个角度确认它是否有效)。如果怀疑从转换视图中提取帧是一个问题,请使用标准方法将当前转换保存为局部变量,将视图转换设置为标识,将帧保存到局部变量集视图转换回保存的值。不幸的是,从滚动视图中提取坐标需要反复试验。然后,将所有内容绘制到图形上下文中可能会再次遇到麻烦,当图像旋转时,所有内容都会升级。但是如果你的视图被正确地绘制,并且你已经清楚地看到了你所需要的,那么也许你应该考虑创建一个视图快照。假设您的图像视图位于另一个视图上,该视图是目标视图的大小和位置。然后随时创建该视图的快照以获取图像。您可能需要将其缩放一点,以将输出图像的大小增加一点…@MaticOblak目标视图是一个小UIView(圆形),它是要裁剪的部分,意味着它是可移动的UIView,由用户设置为裁剪图像中的特定部分view@MaticOblak我做到了!!!!!看到我的答案了吗