• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将 UIScrollViewPoint 转换为 UIImage + ZOOM + ROTATE

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:33:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要裁剪加载在 UIScrollview 中的 UIImage 以及另一个 UIView 的矩形,该矩形也在 UIScrollView

下面是 View 层次结构

 --> 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 与我在模拟器中看到的一样

对于 converting 目标 View 点到 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 缩放和旋转的

如果您需要更多信息,请询问



Best Answer-推荐答案


我正在回答我自己的问题。

感谢 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];

关于ios - 将 UIScrollViewPoint 转换为 UIImage + ZOOM + ROTATE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45854149/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap