UIImageView截图剪辑与子视图

时间:2015-02-20 23:47:59

标签: objective-c xcode uiview uiimageview uiimage

我正在尝试截取一个UIImageView,它具有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;
}

之前(定位):

Before

后:

After

2 个答案:

答案 0 :(得分:0)

我想问题是你看到所选内容的四分之一。

尝试替换

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

用这个

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

答案 1 :(得分:0)

我找到了我的问题的答案 - 我希望这可以帮助任何人,因为它困扰了我一段时间:

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

- (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;
}