1. 程式人生 > >iOS圖層混合簡單優化方案

iOS圖層混合簡單優化方案

圖層混合檢視:執行專案,在Xcode9->Debug->view Debugging->Rendering勾選Color Blened Layers 

1.label的處理

label.backgroundColor  = [UIColor whiteColor];

label.layer.masksToBounds = YES;

控制元件不發生形變的時候masksToBounds是不會觸發離屏渲染的

2.圖片處理

/**
 重新繪製image

 @param sourceImage <#sourceImage description#>
 @param size <#size description#>
 @return <#return value description#>
 */
- (UIImage *)drawImage:(UIImage *)sourceImage size:(CGSize)size color:(UIColor *)color {
    if (!sourceImage) {
        return nil;
    }
    
    CGRect bounds = CGRectMake(0, 0, size.width, size.height);
    // 開啟圖形上下文 size:繪圖的尺寸 opaque:不透明 scale:螢幕解析度係數,0會選擇當前裝置的螢幕解析度係數
    UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0);
    // 背景顏色填充
    [color setFill];
    UIRectFill(bounds);
    
    [sourceImage drawInRect:bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

經驗來自: