首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGContextAddEllipseInRect到CGImageRef到CGImageMaskCreate到CGContextClipToMask

CGContextAddEllipseInRect到CGImageRef到CGImageMaskCreate到CGContextClipToMask
EN

Stack Overflow用户
提问于 2011-07-21 05:36:19
回答 1查看 3.4K关注 0票数 0

我还没有在互联网上找到一个例子来教我如何动态创建一个圆,然后用这个圆来裁剪一个UIImage。

这是我的代码,不幸的是它没有给我想要的结果。

代码语言:javascript
复制
//create a graphics context
UIGraphicsBeginImageContext(CGSizeMake(243, 243));
CGContextRef context = UIGraphicsGetCurrentContext();

//create my object in this context
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 243, 243));
CGContextSetFillColor(context, CGColorGetComponents([[UIColor whiteColor] CGColor]));
CGContextFillPath(context);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//create an uiimage from the ellipse


//Get the drawing image
CGImageRef maskImage = [image CGImage];

// Get the mask from the image
CGImageRef maskRef = CGImageMaskCreate(CGImageGetWidth(maskImage)
                                    , CGImageGetHeight(maskImage)
                                    , CGImageGetBitsPerComponent(maskImage)
                                    , CGImageGetBitsPerPixel(maskImage)
                                    , CGImageGetBytesPerRow(maskImage)
                                    ,  CGImageGetDataProvider(maskImage)
                                    , NULL
                                    , false);


//finally clip the context to the mask.

CGContextClipToMask( context , CGRectMake(0, 0, 243, 243) , maskRef );


//draw the image
[firstPieceView.image drawInRect:CGRectMake(0, 0, 320, 480)];
// [firstPieceView drawRect:CGRectMake(0, 0, 320, 480)];

//extract a new image
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

NSLog(@"self.firstPieceView is %@", NSStringFromCGRect(self.firstPieceView.frame));
UIGraphicsEndImageContext();


self.firstPieceView.image = outputImage;

如果您能指点我,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-21 13:14:19

我怀疑你需要更好地重新表达你的问题。无论您想做什么,都有大量的示例代码。

下面是如何实现一个自定义的UIView子类来将am图像裁剪到一个椭圆:

代码语言:javascript
复制
- (void)drawInRect:(CGRect)rect {
  UIImage image;// set/get from somewhere
  CGImageRef imageRef = [image CGImageRef];
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextAddEllipseInRect(context, self.bounds);
  CGContextClip(context);
  CGContextDrawImage(context, self.bounds, imageRef);
}

买者自负

编辑(一天后,空闲时间产生):

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect {
  // we're ignoring rect and drawing the whole view
  CGImageRef imageRef = [_image CGImage]; // ivar: UIImage *_image;
  CGContextRef context = UIGraphicsGetCurrentContext();

  // set the background to black
  [[UIColor blackColor] setFill];
  CGContextFillRect(context, self.bounds);


  // modify the context coordinates,
  // UIKit and CoreGraphics are oriented differently
  CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0, CGRectGetHeight(rect));
    CGContextScaleCTM(context, 1, -1);

    // add clipping path to the context, then execute the clip
    // this is in effect for all drawing until GState restored
    CGContextAddEllipseInRect(context, self.bounds);
    CGContextClip(context);

    // stretch the image to be the size of the view
    CGContextDrawImage(context, self.bounds, imageRef);
  CGContextRestoreGState(context);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6768862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档