- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法在 Internet 上找到一个示例来教我如何即时创建一个圆圈,然后使用该圆圈来剪辑 UIImage。
这是我的代码,不幸的是它没有给我想要的结果。
//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;
如有任何指示,我将不胜感激。
最佳答案
我怀疑您需要更好地改写您的问题。无论您想做什么,这里都有大量示例代码。
下面是如何实现自定义 UIView
子类以将图像剪裁成椭圆:
- (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);
}
买者自负
编辑(一天后,空闲时间产生):
- (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);
}
关于iphone - CGContextAddEllipseInRect 到 CGImageRef 到 CGImageMaskCreate 到 CGContextClipToMask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6768862/
我发现 CGBitmapContextCreateImage() 创建了一个图像,该图像不一定总是与 CGContextClipToMask() 兼容的掩码。但是,当使用 CGImageMaskCre
以下是在上下文中绘制 UIImage 的具体方法: UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f); [someUIImage drawI
这个问题让我很生气。我不知道出了什么问题。但是下面的代码把图像颠倒了。它实际上进行了垂直翻转,我不知道为什么。 UIFont *font = [UIFont fontWithName:fontName
我到处找遍了,还是没有结果。我正在线程中加载一些图像,由于 UIKit 不是线程安全的,我必须将图像存储为 CGImageRefs,但我不知道如何执行此操作。我以前没有玩过任何 Quartz 的东西,
我正在尝试从屏幕缓冲区中读取像素,我正在使用 CGDisplayCreateImage 创建一个 CGImageRef,但 CGImageGetWidth 的值> 和 CGImageGetByte
我正在为我的一个应用程序屏幕创建模糊图像,为此我使用以下代码 UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer
所以我知道您可以使用 UIImage 从文件中获取 CGImage... UIImage *img = [UIImage imageNamed:@"name.bmp"]; [img CGImage];
好的,使用 coregraphics,我正在构建一个图像,稍后将在 CGContextClipToMask 操作中使用。它看起来像下面这样: UIImage *eyes = [UIImage
我想创建一个 CGImageRef 并绘制指向它的点。 使用什么上下文来创建空的 CGImageRef 并能够在其上进行绘制。CGContextRef 还是 CGBitmapContextRef? 如
我正在通过CGImageSource从TIFF文件创建CGImageRef,在另一种情况下通过CGDataProvider从原始位图数据创建CGImageRef,在另一种情况下通过NSImage从PD
该项目是一个具有核心图形支持的通用 C++ 插件。我无法成功将 PicHandle 转换为 CGImageRef。从 CFDataRef 保存图像会导致图像损坏。 if(pic) {
我需要使用 CGImageSourceCreateThumbnailAtIndex(...) 创建数千个 CGImage 对象。 问题是,当使用简单的 CGDataProviderCreateWith
我正在构建一个 Iphone 应用程序,需要将 UIImage 转换为 CGImageRef。我不知道该怎么做,并且希望得到一些帮助。我没有处理图像的背景...:) 最佳答案 它就像image.CGI
MTLTexture 之间的主要区别是什么?对比 CGImageRef ?我们什么时候需要使用MTLTexture而不是 CGImageRef (反之亦然)? 我有一个应用程序(比如视频游戏),它可以
我剪掉了一段 .c 代码,但我不想将其移植到 Objective-C。这个.c文件在iPhone平台上工作,但在OS X上不行。看来编译器不知道如何处理 #import 所以它不知道CGImageR
我能知道 CGPoint 是否在 CGImageRef 内吗?或者我应该将 CGImageRef 转换成其他格式? 更新 maskcontext = CGBitmapContextCreate(cur
我正在制作一个从电影中去除帧的程序。这在 iOS 7 上的 Xcode 5.0 模拟器中。我试图放入这个数组中的对象数量是 447。(似乎没有那么多)。 由于数组大小不够大导致内存泄漏,我的程序不断崩
我做了一个上下文,然后是一个渐变。之后,我将根据上下文进行绘制。然后我从上下文中收到一个渐变图像,它是正确的,我可以在调试器中看到它。但是当我尝试获取特定值的像素颜色时,它是不正确的。感谢您的帮助,谢
我正在考虑优化从 CGImage 获取像素数据的例程。目前这样做的方式(非常低效)是创建一个新的 CGContext,将 CGImage 绘制到上下文中,然后从上下文中获取数据。 我有以下优化例程来处
我正在用核心图形绘制图像,而不是我想将它用作另一个图像的 mask ,但只是得到清晰的矩形图像。 看看我的裁剪功能有什么问题? UIGraphicsBeginImageContext(CGSi
我是一名优秀的程序员,十分优秀!