【问题标题】:UIImageView screenshot clipping with subviews带有子视图的 UIImageView 截图剪辑
【发布时间】:2015-04-22 19:03:17
【问题描述】:

我正在尝试截屏一个 UIImageView,该 UIImageView 具有可移动、可缩放和可旋转的 alpha(爱心,心脏具有 alpha 透明度,黑色边缘) - 在不移动的 UIImageView 之上。

我想要完成的是允许用户将心形移到顶部(已经让代码为此工作) - 然后当他们单击下一步按钮时,它会截取心形的屏幕截图,并且捕捉下面的图像。我有这段代码通过截图图像传递给一个segue。这是我到目前为止所拥有的,这就是结果。

这是我当前的代码和之前/之后的代码:

moveableMaskImageView 是 Heart UIImageView
outletPreview 是下面的 UIImageView

#pragma screenshotImage:
- (UIImage *)screenShotImage {
CGRect rect = [moveableMaskImageView frame];
NSLog(@"Screenshot X: %f Screenshot Y: %f Screenshot W: %f Screenshot H: %f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[outletPreview.layer renderInContext:context];
[moveableMaskImageView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return capturedImage;
}

之前(定位):

之后:

【问题讨论】:

  • 我们应该帮助您解决什么问题?能否请您明确说明问题所在?
  • 我正在尝试使结果图像成为心脏图像的框架和平移 - 因此心脏图像之外的所有内容都会被剪掉,留下心脏和剪裁的子视图

标签: objective-c xcode uiview uiimageview uiimage


【解决方案1】:

我找到了问题的答案 - 我希望这对任何人都有帮助,因为它让我困惑了一段时间:

我没有使用我的屏幕截图功能来剪辑图像 - 我使用了我在互联网上找到的这段代码:

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect {

UIGraphicsBeginImageContext(outletContainer.frame.size);/// use your screen or photo frame
[outletContainer.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];

/*    no origin .....
 UIGraphicsBeginImageContext(tempview.frame.size);
 [[self.photo layer] renderInContext:UIGraphicsGetCurrentContext()];
 cropped= UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 */
return cropped;
}

【讨论】:

    【解决方案2】:

    我想问题是你看到了四分之一的选择。

    尝试替换

    这个

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);
    

    有了这个

    CGSize *size = CGSizeMake(rect.size.x *2, rect.size.y *2);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 1970-01-01
      • 2012-04-18
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多