将UIScrollViewPoint转换为UIImage + ZOOM + ROTATE
我需要裁剪UIImage
,它在UIScrollview
加载了另一个UIView
矩形,它也在UIScrollView
以下是视图层次结构
--> View --> UIScrollView --> viewBase (UIView) --> UIImageView -> (Zoomed & rotated ) --> UIView (Target View)(Movable User can move anywhere in scrollview to crop rect)
我的图像是旋转和缩放我需要在TargetView
获取图像的精确部分
我在context
使用旋转绘制UIImage
是代码
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
为了将目标视图点转换为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];
我认为问题是我们不能在Transform Apply之后使用Rect
请帮助我裁剪UIImage
,它在相同的层次结构上从rect缩放和旋转
如果您需要更多信息,请询问
我在回答我自己的问题。
感谢Matic给出了一个想法
我改变了逻辑
我已经实现了我想要的function
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];
- 没有UICollectionViewLayoutAttributes实例为-layoutAttributesForSupplementaryElementOfKind:
- QTMovieLayer具有非零“内容”的iOS替代品?
- FFMPEG编译的库和iOS
- 认识到一个圆圈手势放弃注释,如何检测圆?
- 如何使程序在继续执行下一个命令之前等待asynchronousNSURLConnection完成?
- NSString中的后缀有序的后缀
- 用于Windows选件的远程iOS模拟器丢失?
- cordova运行与ios错误..错误代码65为命令:xcodebuild与args:
- 打开Apple Maps以进行导航并设置目的地名称