我想在不使用QuartzCore的情况下为UIImageView添加圆角以避免UIScrollView中的性能问题,所以我解决了它:
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
[path addClip];
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear); CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
CGContextAddPath(context,path.CGPath);
CGContextClip(context);
CGContextClearRect(context,CGRectMake(0,0,width,height));
[_image drawInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
遗憾的是,在drawRect中调用,这需要一点处理时间,这会在UIScrollView中滚动时产生滞后.因此,我试图在dispatch_async的帮助下在一个单独的线程中处理它.这消除了滞后,一切都顺利进行.但现在我有另一个问题.我在调试器中收到许多无效的上下文消息,因为当线程异步启动图像处理时,GraphicsContext并不总是存在.有没有办法处理我的图像中的圆角而不会得到无效的上下文消息?请注意,我不想使用QuarzCore的cornerRadius或mask-functions.
以下是如何在上下文中绘制UIImage:
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[someUIImage drawInRect:CGRectMake(...)];
[someOtherUIImage drawInRect:CGRectMake(...)];
[someOtherUIImage drawInRect:CGRectMake(...)];
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
没问题.但是,假设我有一个以某种方式构建的CGImageRef.
CGImageRef happyImageRef = CGImageCreateWithImageInRect(blah blah);
Run Code Online (Sandbox Code Playgroud)
我想把它绘制到上下文中.
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[ happyImageRef .. somehowDrawInRect:CGRectMake(...)];
UIImage *yourResult = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
你是怎样做的?
要清楚,显然你可以将它转换为UIImage
UIImage *wasteful = [UIImage imageWithCGImage:happyImageRef];
Run Code Online (Sandbox Code Playgroud)
但这看起来非常低效.怎么做?
我确定我很困惑或遗漏了一些简单的东西,谢谢.
我的目的是使用AVFoundation捕获和显示(使用叠加视图)捕获的图像 - 应该与预览图层中的图像相同.
使用iPhone 4"屏幕尺寸很好,因为它只是调整捕获图像的大小.但是,使用iPhone 3.5"屏幕尺寸证明更复杂 - 需要调整大小和裁剪.
虽然我的代码适用于两个摄像头位置(正面和背面),但调整背面摄像头拍摄图像的代码有一些性能问题.在调整图像大小时,我已将性能问题缩小到更大的图像上下文.需要更大的图像上下文来保持图像的"视网膜"质量,而前置摄像头非常差,所捕获的图像无论如何都不具有"视网膜"质量.
有问题的代码是:
UIGraphicsBeginImageContext(CGSizeMake(width, height))
// where: width = 640, height = 1138
// image dimensions = 1080 x 1920
image.drawInRect(CGRectMake(0, 0, width, height))
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
我已经四处搜索,但找不到另一种更有效的方法.任何人都可以帮我克服这个性能问题吗?谢谢
我知道还有其他几种方法可以做到这一点; 我不想导入任何我不需要的东西.如果有人可以帮助我使用他的代码,那就太好了.
目前,仅保存没有水印图像的原始图像.
extension UIImage {
class func imageWithWatermark(image1: UIImageView, image2: UIImageView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(image1.bounds.size, false, 0.0)
image2.layer.renderInContext(UIGraphicsGetCurrentContext()!)
image1.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
func addWatermark() {
let newImage = UIImage.imageWithWatermark(imageView, image2: watermarkImageView)
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
}
Run Code Online (Sandbox Code Playgroud)
编辑:我已经在保存的图像上出现水印.
我不得不切换图层的顺序:
image1.layer.renderInContext(UIGraphicsGetCurrentContext()!)
image2.layer.renderInContext(UIGraphicsGetCurrentContext()!)
Run Code Online (Sandbox Code Playgroud)
但是,它并没有出现在正确的位置.它似乎总是出现在图像的中心.
我的故事板上有 3 个视图,viewA, viewB, viewC。
我试图屏幕捕获只有两个视图,因为它们出现在屏幕上的当前位置,viewB并且viewC.
问题是,当我渲染它们时,捕获的结果图像显示viewB并且viewC在不正确的位置,视图的位置改变移动左上角 (0, 0),见图像。
如何更正下面的代码,以便我可以使用下面的实现捕获视图viewB并viewC准确地将它们定位在视图上renderInContext?
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
self.viewB.layer.renderInContext(UIGraphicsGetCurrentContext()!)
self.viewC.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
I'm rendering multiple layers to get one final image. One of the images contains a face, and another one contains a background with a transparent gradient around the face so that the real background is hidden. In iOS6 it works perfectly but it is creating a weird effect with the transparent gradient in iOS7.
The code:
CGRect rect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[faceImageView.layer renderInContext:context];
[fakeBackgroundImageView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
The results in iOS6 …
我正在裁剪图像:
UIGraphicsBeginImageContext(croppingRect.size)
let context = UIGraphicsGetCurrentContext()
context?.clip(to: CGRect(x: 0, y: 0, width: rect.width, height: rect.height))
image.draw(at: CGPoint(x: -rect.origin.x, y: -rect.origin.y))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
裁剪后的图像有时在右边框和下边框上有 1 像素的白边。右下角放大后可以看到各个像素如下所示。显然,边框不是纯白色,而是可能来自后期压缩的白色阴影。
这个白边伪影从何而来?
我想截取一个视图并从中创建一个 UIImage 。我希望在我的图像中保留视图的透明度属性。我在创建 UIImage 的扩展后尝试了这种方法,但是当上传到服务器时,结果图像中的背景不透明。
如果我做错了什么,请帮助或指出我!!这意味着生成的 png 没有透明度。
class func createTransparentImageFrom(label: UILabel, imageSize: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(imageSize, false, 2.0)
let currentView = UIView.init(frame: CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height))
currentView.backgroundColor = UIColor.clear
currentView.addSubview(label)
currentView.layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img!
}
Run Code Online (Sandbox Code Playgroud) UITableViewCell我使用以下代码在 a 上绘制一条水平线和一条垂直线,它在 iOS7 上运行良好。它在 Objective-C 中。
在 的子类中,
@property (strong, nonatomic) NSMutableArray *columns;
- (void)drawRect:(CGRect)rect
{
// In iOS7, you have to set the cell's background color to clear otherwise the drawing lines won't appear
self.backgroundColor = [UIColor clearColor];
// Drawing the vertical line
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.75);
for (int i = 0; i < self.columns.count; i++) {
CGFloat f = [((NSNumber*) [self.columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 10);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height …Run Code Online (Sandbox Code Playgroud) 我正在开发一个着色应用程序,但是我无法实现“撤消”按钮。我不确定这种方法,我尝试过实现 NSUndoManager,但无法让它有效工作。我的方法可能是不正确的。我将非常感谢基于我的示例使用代码的答案。
\n\n- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {\n\n mouseSwiped = NO;\n UITouch *touch = [touches anyObject];\n lastPoint = [touch locationInView:self.view];\n} \n\n- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {\n\n mouseSwiped = YES;\n UITouch *touch = [touches anyObject];\n CGPoint currentPoint = [touch locationInView:self.view];\n\n UIGraphicsBeginImageContext(self.view.frame.size);\n [self.tempImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];\n\n //get the current touch point and then draw a line with CGContextAddLineToPoint from lastPoint to currentPoint. You\xe2\x80\x99re right to think that this approach will produce a series of straight lines, but the lines are …Run Code Online (Sandbox Code Playgroud) 我有以下代码来创建多色 ImageContext:
UIGraphicsBeginImageContext(self.view.frame.size);
[self.imageToDelete drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGColorRef mycolor;
mycolor = [[UIColor blueColor] CGColor];
CGPoint mid = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), mid.x, mid.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), mid.x, mid.y+50);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),3);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), mycolor);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
mycolor = [[UIColor whiteColor] CGColor];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), mid.x, mid.y+50);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), mid.x, mid.y+75);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),3);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), mycolor);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
mycolor = [[UIColor cyanColor] CGColor];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), mid.x, mid.y+75);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), mid.x, mid.y+100);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),3);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), mycolor);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.imageToDelete = UIGraphicsGetImageFromCurrentImageContext();
[self.mainImage drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeCopy …Run Code Online (Sandbox Code Playgroud) 我试图在一个单元UITableView格内截取屏幕截图,但附加代码我只能截取单元格界限.问题在于UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f).我只能制作rect大小的截图,不能控制rect和的起源
rect = [cell bounds].所以请建议我一些想法.
{
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:path];
__block CGRect rect = [cell bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[cell.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Run Code Online (Sandbox Code Playgroud) 这段代码:
let rect = CGRectMake( 0.0, 0.0, 16.0, 16.0 )
let context = UIGraphicsGetCurrentContext()
UIGraphicsBeginImageContext( rect.size )
CGContextSetFillColorWithColor( context, color.CGColor )
CGContextFillRect( context, rect )
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)
正在抛出这些错误:
MakeImage[60890] <Error>: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
MakeImage[60890] <Error>: CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Run Code Online (Sandbox Code Playgroud)
CGRect 大小似乎很好,其他帖子建议作为问题。那么还有什么设置不正确,请?谢谢你。
ios ×9
swift ×7
uiimage ×4
objective-c ×3
asynchronous ×1
calayer ×1
cgimageref ×1
cgrect ×1
drawing ×1
drawinrect ×1
ios6 ×1
ios7 ×1
screenshot ×1
sprite-kit ×1
uibezierpath ×1
uitableview ×1
undo ×1