2017-08-24 123 views
2

我需要裁剪它在UIScrollview装载的另一个UIView一些RECT这也是UIScrollView转换UIScrollViewPoint到的UIImage + ZOOM +旋转

以下UIImage是视图层次

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

我的形象旋转&放大我需要得到确切图片的部分TargetView

我正在绘制UIImage with rotati上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的一样,我可以在模拟器

看到转换点的目标观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被放大,如果你从矩形在同一层次

旋转需要更多信息请问

+0

什么是目标视图和图像视图之间的关系是相同的功能逻辑

?如果UIImage具有方向性(尝试在运行时拍摄人像照片并将其通过几个角度以确认其真正起作用),我很怀疑旋转代码会起作用。如果您怀疑从转换视图中提取帧是一个问题,那么使用标准方法将当前变换保存到局部变量,将视图变换设置为标识,将帧保存为局部变量集视图变换回保存的值。 –

+0

不幸的是,从滚动视图中提取坐标是一个试验和错误过程。然后将所有东西绘制到图形上下文中可能会再次出现麻烦,并且在图像旋转时所有内容都会升级。但是,如果您的视图被正确绘制,并且您已经确切地看到了您需要的内容,那么也许您应该考虑创建视图快照。假设您的图像视图位于另一个视图上,即视图的大小和位置。然后随时创建该视图的快照以获取图像。您可能需要稍微扩展一下以增加输出图像的大小... –

+0

@MaticOblak目标视图是一个小的UIView(圆圈),它是** crop的一部分**表示它是可移动的UIView,它由用户在图像视图中裁剪特定部分 –

回答

0

我在回答我自己的问题。

由于马蒂奇给了一个想法

我改变了我已经实现了我寻找

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