将UIScrollViewPoint转换为UIImage +缩放+旋转 [英] Convert UIScrollViewPoint to UIImage + ZOOM + ROTATE

查看:177
本文介绍了将UIScrollViewPoint转换为UIImage +缩放+旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要裁剪在UIScrollview中加载的UIImage,并在UIScrollView

I need to crop UIImage which is loaded in UIScrollview with some rect of Another UIView which is also in UIScrollView

以下是视图层次结构

 --> View
     -->  UIScrollView
        --> viewBase (UIView)
             --> UIImageView -> (Zoomed & rotated )
             --> UIView (Target View)(Movable User can move anywhere in scrollview  to crop rect)

我的图像旋转了&放大我需要在TargetView

My Image is Rotated & Zoomed I need to get exact part of image in TargetView

我正在绘制UIImage并在context上旋转,以下是代码

I am drawing UIImage with rotation on context following is code

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与在Simulator

And it works fine . I am getting Rotated UIImage same as i can see in Simulator

对于转换目标视图到UIImage的点,我使用下面的代码,该代码不起作用

For converting Point of Target View to UIImage I use following code which is NOT WORKING

 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

I think issue is we can't use Rect after Transform Apply

请帮助我裁剪UIImage,该UIImage在同一层次结构上从rect缩放并旋转

Please Help me to crop UIImage which is zoomed and rotated from rect on same Hierarchy

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

If you need more info pls ask

推荐答案

我正在回答自己的问题.

I am answering my own question .

感谢Matic提出一个想法

Thanks to Matic for giving a idea

我改变了逻辑

我已经达到了我想要的功能

I have achieved same functionality what i looking for

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

这篇关于将UIScrollViewPoint转换为UIImage +缩放+旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆