簡體   English   中英

RenderInContext在iOS7上非常慢

[英]RenderInContext is incredibly slow on iOS7

我正在應用程序中實現“臨時”功能。 用戶刮擦屏幕並看到“下方”圖像。

在touchesMoved上:我更新蒙版圖像並將其應用於圖層。 一般的代碼是這樣的:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint cPoint = [touch locationInView:self];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    imageView.image = _maskImage;

    // ... add some subviews to imageView corresponding to touch manner

    _maskImage = [UIImage imageFromLayer:imageView.layer];

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    _maskImageView.image = _maskImage;
    _viewWithOurImage.layer.mask = _maskImageView.layer;
}

我從CALayer使用代碼(UIImage上的類別)獲取UIImage:

+ (UIImage*)imageFromLayer:(CALayer*)layer
{
    UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, 0);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outputImage;
}

該代碼在iOS6上完美運行(在iPhone 4s和iPad2上測試),完全沒有滯后。

但是,當我在iOS7(xcode4或xcode5)上運行它時,它的運行速度非常緩慢且緩慢。 我使用了一個時間分析器,它顯然指向renderInContext:行。

然后我嘗試了以下代碼:

...
    if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
        _maskImage = [UIImage imageFromLayer:imageView.layer];
    else
        _maskImage = [UIImage imageFromViewIniOS7:imageView];
...

+ (UIImage*)imageFromViewIniOS7:(UIView*)view
{
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0);
    CGContextSetInterpolationQuality(UIGraphicsGetCurrentContext(), kCGInterpolationNone);

    // actually there is NSInvocation, but I've shortened for example
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];

    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outputImage;
}

而且它仍然很慢。 在iPhone 4s(與iOS6相同),新的iPod5和iPad3上進行了測試。

我究竟做錯了什么? 顯然這是iOS7的問題...

我將不勝感激任何建議。

我會建議您嘗試其他方法,抱歉地說, touchesMoved函數在IOS7中運行緩慢,您的代碼沒有錯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM