1

我正在尝试生成具有子视图的 UIView 的屏幕截图CATransform3DMakeRotation。屏幕截图已生成,但不包含旋转。

有可能实现这一目标吗?

实际视图:

在此处输入图像描述

截图图像

在此处输入图像描述

使用以下调用水平翻转视图...

  currentView.layer.transform = CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0f));

截屏代码

+ (UIImage *) imageWithView:(UIView *)view
{
    CGSize screenDimensions = view.bounds.size;

    // Create a graphics context with the target size
    // (last parameter takes scale into account)
    UIGraphicsBeginImageContextWithOptions(screenDimensions, NO, 0);

    // Render the view to a new context
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();

    return img;
}
4

6 回答 6

6

“renderInContext”仅适用于仿射变换。所以像这样将3D变换转换为仿射变换

currentView.layer.affineTransform = CATransform3DGetAffineTransform(CATransform3DConcat(currentView.layer.transform,CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0f)));
于 2013-10-08T09:54:43.330 回答
1

这可能有效,请尝试:

  CGRect grabRect = CGRectMake(40,40,300,200);

    //for retina displays
  if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale);
  } else {
  UIGraphicsBeginImageContext(grabRect.size);
  }
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
  [self.view.layer renderInContext:ctx];
  UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
于 2013-10-08T05:24:08.717 回答
1

试试这个代码

CGSize newSize = CGSizeMake(yourview.frame.size.width , yourview.frame.size.height);
UIGraphicsBeginImageContextWithOptions(newSize,YES,2.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[yourview.layer renderInContext:context];
[yourview drawRect:yourview.frame];

UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
于 2013-09-30T14:15:15.910 回答
0

我已经在我的一个应用程序中实现了这一点,方法是先进行一些调整,例如首先捕获整个屏幕的屏幕截图,然后用我需要的所需帧裁剪它这里是我的应用程序中的示例代码。

- (UIImage *) croppedPhoto
{
    [imgcropRectangle setHidden:TRUE];
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Create bitmap image from original image data,
    // using rectangle to specify desired crop area
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], self.imgcropRectangle.frame);
    UIImage *result = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    [imgcropRectangle setHidden:FALSE];
    return result;
}

这里 imgcropRectangle 是 UIImageView 的对象,它定义了我想要的矩形,所以我使用它的框架从全屏裁剪到想要的输出。希望它会帮助你:)

于 2013-10-05T12:05:13.570 回答
0

尝试渲染view.layer.presentationLayer而不是view.layer

于 2013-10-08T10:19:27.847 回答
0

使用它并在通过视图之前检查它的子视图:

+ (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width, view.frame.size.height));
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
return viewImage
}
于 2013-10-08T14:05:55.457 回答