简体   繁体   English

从UIScrollView中以完整分辨率捕获缩放的UIImageView

[英]Capture scaled UIImageView from within UIScrollView at full resolution

I have a UIImageView that has an image 640x1136. 我有一个UIImageView ,具有图像640x1136。 I want users to be able to scale and translate the image inside a cropping area (also 640x1136) then re-save the new cropped one at 640x1136 (pinch/pull/slide) so I stuck it in a UIScrollView and shrunk both of them down to around 100x178 and added scrolling capabilities -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 我希望用户能够在裁切区域(也为640x1136)中缩放和转换图像,然后将新裁切后的图像重新保存为640x1136(收缩/拉动/滑动),因此我将其粘贴在UIScrollView并将它们缩小到100x178左右,并增加了滚动功能-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

Everything worked! 一切正常! I could move the image around, shrink and scale it, it was getting "clipped" at the edge of the subview! 我可以四处移动图像,缩小并缩放它,它在子视图的边缘被“剪切”了! So now I am stuck trying to figure out how to snap a screenshot of it at max resolution? 因此,现在我不得不尝试找出如何以最大分辨率捕捉其屏幕快照? I can snap a screenshot with CGContext and it shows the proper "cropped" image section, but the resolution is 100x178, so I scale the whole thing up to 640x1136 then snap a screenshot but the problem is the content inside the scroll view doesn't scale up with it, so now the crop is all messed up! 我可以使用CGContext捕获屏幕截图,并且显示正确的“裁剪”图像部分,但是分辨率为100x178,因此我将整个图像缩放到640x1136,然后捕获屏幕截图,但是问题是滚动视图中的内容不正确扩大规模,现在收成就糟透了! Are there any good solutions out there? 有没有好的解决方案?


Here is the code I tried; 这是我尝试过的代码; it didn't work: 它不起作用:

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 1136), YES, 0.0);
    UIImageView *theImageToCrop = [[UIImageView alloc] initWithFrame:CGRectMake(0-(theScrollView.contentOffset.x*(640/theScrollView.frame.size.width)), 0-(theScrollView.contentOffset.y*(1136/theScrollView.frame.size.height)), theScrollView.contentSize.width*(640/theScrollView.frame.size.width), theScrollView.contentSize.height*(1136/theScrollView.frame.size.height))];
    theImageToCrop.contentMode = UIViewContentModeScaleAspectFill;
    theImageToCrop.image = originalImageView.image;
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theImageToCrop.layer renderInContext:context];
    croppedImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

I can already tell the problem is that the image is not being translated when I take the final screenshot. 我已经可以说出问题了,当我拍摄最终的屏幕截图时,图像没有被翻译。 It is scaling to the proper size that it was scaled to in the UIImageView but the content offset is not being set. 它可以缩放到在UIImageView缩放到的适当大小,但是未设置内容偏移量。 I am setting it in the frame I create with CGRectMake but I have a feeling that when I use this code: [theImageToCrop.layer renderInContext:context]; 我将其设置在使用CGRectMake创建的框架中,但是当我使用此代码时,我有一种感觉: [theImageToCrop.layer renderInContext:context]; the size is maintained but the origin is reset to (0,0) . 保持大小,但原点重设为(0,0) How can I keep the size and origin? 如何保持大小原产地?

Couldn't figure out how to translate after renderInContext is applied to a layer... so rather than sticking the scaled UIImageView's layer into renderInContext I created a UIView the same size at origin (0,0) then I added the imageView as a subview to the UIView I just created and translated it within the view THEN I applied renderInContext to that view! 在将renderInContext应用于图层后无法弄清楚如何翻译...因此,与其将缩放的UIImageView的图层粘贴到renderInContext我在原点(0,0)创建了一个相同大小的UIView,然后将renderInContext添加为子视图到我刚刚创建的UIView并在视图中转换它,然后将renderInContext应用于该视图!

UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 1136), YES, 0.0);
UIView *theImageHolder = [[UIView alloc] initWithFrame:CGRectMake(0,0,640,1136)];
UIImageView *theImageToCrop = [[UIImageView alloc] initWithFrame:CGRectMake(0-(theScrollView.contentOffset.x*(640/theScrollView.frame.size.width)), 0-(theScrollView.contentOffset.y*(1136/theScrollView.frame.size.height)), theScrollView.contentSize.width*(640/theScrollView.frame.size.width), theScrollView.contentSize.height*(1136/theScrollView.frame.size.height))];
theImageToCrop.contentMode = UIViewContentModeScaleAspectFill;
theImageToCrop.image = originalImageView.image;
CGContextRef context = UIGraphicsGetCurrentContext();
[theImageHolder addSubview:theImageToCrop];
[theImageHolder.layer renderInContext:context];
croppedImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

edit: It should be noted that if the user has NOT made any adjustments to the image rather than leaving the image as is when preforming the crop it will return an image of width:height = 0:0 ... this is because contentSize is 0:0 ... To fix this instead of using contentSize in the UIImageView CGRectMake code use a float variable and define the float variables in a way that if they are equal to zero they just get set to the size of your UIScrollView frame (the same size they would be had the user not pinched them at all to scale! 编辑:应注意,如果用户未对图像进行任何调整,而是在进行裁剪时未按原样保留图像,则它将返回width:height = 0:0的图像...这是因为contentSize为0:0 ...为了解决此问题,而不是在UIImageView CGRectMake代码中使用contentSize,请使用float变量并以如下方式定义float变量:如果它们等于零,则只需将其设置为UIScrollView框架的大小即可(如果用户完全不按比例缩放它们,它们的尺寸将相同!

float scaleWidth = imageAdjustView.contentSize.width;
float scaleHeight = imageAdjustView.contentSize.height;
if (scaleWidth == 0) {
    scaleWidth = imageAdjustView.frame.size.width;
}
if (scaleHeight == 0) {
    scaleHeight = imageAdjustView.frame.size.height;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM