从旋转的calayeer获取uiimage

时间:2012-01-26 00:04:20

标签: iphone quartz-2d

我正试图从我之前旋转的一个calayer获得一个uiimage:

//Creates the rotation affine transform
m_transformada = CGAffineTransformIdentity;
m_transformada = CGAffineTransformRotate(m_transformada, M_PI / 4);
// Apply the affine transform to a UIVIewImage object
m_PhotoView.transform = m_transformada;

// Get's the UIImage from the UIViewImage
UIGraphicsBeginImageContext([m_PhotoView.layer frame].size);
[m_PhotoView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

outputImage没有旋转......有谁知道为什么? 谢谢,

1 个答案:

答案 0 :(得分:1)

图层上的变换会影响其超级图层中的几何图形,而不会影响图层本身。

这样的东西会起作用(这意味着混合和/或以其他方式改编):

- (UIImage*)renderToImageRotated:(float)scale {
  float tx = self.frame.size.width;
  float ty = self.frame.size.height;
  CGSize size = CGSizeMake(self.frame.size.height, self.frame.size.width);
  UIGraphicsBeginImageContextWithOptions(size, NO, scale);
  CGContextRef c = UIGraphicsGetCurrentContext();
  CGContextTranslateCTM (c, ty/2, tx/2);
  CGContextRotateCTM(c, M_PI/2);
  CGContextTranslateCTM (c, -tx/2, -ty/2);
  [self.layer renderInContext:c];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();  
  return image;
}