用路径提取UIImage

时间:2012-09-04 14:38:58

标签: ios objective-c ios5

如何使用路径提取UIImage?

我需要将路径内的内容转换为新的UIImage。我需要的是旋转矩形的内容。这是我用来获取矩形角的代码。 (x,y =图像中心。宽度,图像高度)。

UIBezierPath* aPath = [UIBezierPath bezierPath];

//1
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@"%f, %f", x+(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)+(width/2)*sinf(A));


//2
[aPath moveToPoint:CGPointMake(
                               x-(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)-(width/2)*sinf(A))];

NSLog(@"%f, %f", x-(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)-(width/2)*sinf(A));


//3
[aPath moveToPoint:CGPointMake(
                               x-(width/2)*cosf(A)+(height/2)*sinf(A),
                               y-(height/2)*cosf(A)-(width/2)*sinf(A))];

NSLog(@"%f, %f", x-(width/2)*cosf(A)+(height/2)*sinf(A),
      y-(height/2)*cosf(A)-(width/2)*sinf(A));


//4
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)+(height/2)*sinf(A),
                               y-(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@"%f, %f", x+(width/2)*cosf(A)+(height/2)*sinf(A),
      y-(height/2)*cosf(A)+(width/2)*sinf(A));



//5
[aPath moveToPoint:CGPointMake(
                               x+(width/2)*cosf(A)-(height/2)*sinf(A),
                               y+(height/2)*cosf(A)+(width/2)*sinf(A))];

NSLog(@"%f, %f", x+(width/2)*cosf(A)-(height/2)*sinf(A),
      y+(height/2)*cosf(A)+(width/2)*sinf(A));
[aPath closePath];

我在想这样的话:A picture of the problem. (这里的形状不同。)我希望黄色部分成为新的UIImage。

1 个答案:

答案 0 :(得分:3)

使用UIBezierPath作为剪切路径。有addClip方法。

有关详细信息,请参阅the Quartz2D Programming Guide,尤其是this part

我们的想法是创建一个新的Bitmap上下文,使用你的路径应用剪辑,然后在这个位图上下文中绘制图像(这将被剪裁),最后从中生成一个UIImage。

此外,您应该自行创建一个CGRect而不进行任何轮换,而不是自己动一些数学来旋转您想要用于裁剪的CGRect,并使用CGAffineTransform旋转它。这样就可以避免使用cos / sin函数自行进行计算,并使代码更易于阅读。


[编辑]以下是一个完整的例子:

  • 生成Bezier路径并对其应用CGAffineTransform旋转
  • 使用此bezier路径剪辑图像

我确实检查了它并且像魅力一样。

// Generate a UIBezierPath of a rounded rect rotated by angle radians
-(UIBezierPath*)computePathWithRect:(CGRect)rect
                       cornerRadius:(CGFloat)cornerRadius
                              angle:(CGFloat)radians;
{
  // Compute basic path
  UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];

  // Apply rotation.
  // Don't forget that rotations are around origin 0,0 so if you want to rotate around the rect's center,
  // you need to apply a translation so that the rect's center is at 0,0, then rotate, and translate it back at its original position.
  CGAffineTransform restoreCenterPosition = CGAffineTransformMakeTranslation(CGRectGetMidX(rect), CGRectGetMidY(rect));
  CGAffineTransform rotateFromOrigin = CGAffineTransformRotate(restoreCenterPosition,radians);
  CGAffineTransform rotateFromRectCenter = CGAffineTransformTranslate(rotateFromOrigin, -CGRectGetMidX(rect), -CGRectGetMidY(rect));
  [path applyTransform:rotateFromRectCenter];

  return path;
}

// Generate a new image from srcImage but clipped with the given path
-(UIImage*)clipImage:(UIImage*)srcImage withPath:(UIBezierPath*)path
{
  UIGraphicsBeginImageContextWithOptions(srcImage.size, NO, 0.0);

  // Add clipping path
  // Actually UIBezierPath has a method for that, that is equivalent to CGContextClip(currentContext, bezierPath.CGPath), so better use it
  [path addClip];

  // Flip coordinates before drawing image as UIKit and CoreGraphics have inverted coordinate system
  CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, srcImage.size.height);
  CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1);
  // Draw image, that will thus be clipped, on the bitmap context
  CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, srcImage.size.width, srcImage.size.height), srcImage.CGImage);

  // Generate final (clipped) image
  UIImage* clippedImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return clippedImage;
}


// Usage example
- (void)doClipping
{
  UIImage* originalImage = ...
  UIBezierPath* path = [self computePathWithRect:CGRectMake(100,100,184,94)
                                    cornerRadius:10.f
                                           angle:30*M_PI/180.f];
  UIImage* clippedImage = [self clipImage:originalImage withPath:path];
  self.resultImageView.image = clippedImage;
}