Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UIBezierPath异步剪裁UIImage_Ios_Asynchronous_Uiimage_Uibezierpath_Uigraphicscontext - Fatal编程技术网

Ios 使用UIBezierPath异步剪裁UIImage

Ios 使用UIBezierPath异步剪裁UIImage,ios,asynchronous,uiimage,uibezierpath,uigraphicscontext,Ios,Asynchronous,Uiimage,Uibezierpath,Uigraphicscontext,我想在不使用QuartzCore的情况下向UIImageView添加圆角,以避免UIScrollView中的性能问题,因此我这样解决了它: UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.cornerRadius, self.corn

我想在不使用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();
不幸的是,在drawRect中调用,这需要一点处理时间,在UIScrollView中滚动时会产生延迟。因此,我尝试在dispatch\u async的帮助下,在一个单独的线程中处理这个问题。这消除了滞后,一切都像它应该的那样顺利进行。但现在我有另一个问题。我在调试器中得到许多无效的上下文消息,因为当线程异步启动图像处理时,GraphicsContext并不总是存在。
有没有一种方法可以在不获取无效上下文消息的情况下处理图像中的圆角?请注意,我不想使用QuarzCore的cornerRadius或mask函数。

您所做的工作超出了您的需要。“无效上下文”消息是由于在调用
UIGraphicsBeginImageContextWithOptions()
之前调用
[path addClip]
引起的。
UIBezierPath
尝试访问线程的当前图形上下文,但您尚未设置

这里有一个获得相同结果的简单方法。请注意,您根本不需要使用
CGContext

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
[path addClip];

[_image drawInRect:rect];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
我希望在不使用QuartzCore的情况下向UIImageView添加圆角,以避免性能问题

一种方法是使图像的角变圆,而不是UIImageView的角。请看我的回答:

我们从任何UIImage开始,通过在剪裁的上下文中绘制来绕过它的角。这不需要“处理时间”,也不会导致性能问题

注意:在表格视图或其他非常快速的滚动或动画情况下,圆角处的透明度可能会导致性能问题。但这是一个更普遍的问题,与如何进行圆角无关;它与我们必须不断合成移动形状这一事实有关在背景上。如果背景是纯色,并且您事先知道它将是什么颜色,则在圆角区域具有背景颜色的位置制作不透明图像更有效。)