iOS-性能优化

2018-12-17  本文已影响13人  00d1ed2b53ae

一、优化CPU

例:把图片的解码操作放到子线程

UIImageView *imgeView = [[UIImageView alloc] init];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        CGImageRef cgImage = [UIImage imageNamed:@""].CGImage;

        CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(cgImage) &
                            kCGBitmapAlphaInfoMask;
        BOOL hasAlpha = NO;
        if (alphaInfo == kCGImageAlphaPremultipliedLast ||
            alphaInfo==kCGImageAlphaPremultipliedFirst ||
            alphaInfo == kCGImageAlphaLast ||
            alphaInfo == kCGImageAlphaFirst) {
            hasAlpha = YES;
        }

        CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host;

        bitmapInfo |= hasAlpha ? kCGImageAlphaPremultipliedFirst :
                            kCGImageAlphaNoneSkipFirst;

        size_t width = CGImageGetWidth(cgImage);
        size_t height = CGImageGetHeight(cgImage);

        //context
        CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 0,
                                                     CGColorSpaceCreateDeviceRGB(), bitmapInfo);

        // draw
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);

        // get CGImage
        cgImage = CGBitmapContextCreateImage(context);

        // into UIImage
        UIImage *newImage = [UIImage imageWithCGImage:cgImage];

        // realease
        CGContextRelease(context);
        CGImageRelease(cgImage);

        // back to the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            imgeView.image = newImage;
        });

    });

二、优化GPU

image.png

三、卡顿检测

这是一个用来监听卡顿的工具包:
https://github.com/UIControl/LXDAppFluecyMonitor

四、耗电优化

作者:GeniusWong
链接:https://www.jianshu.com/p/c7dfb6cdb9bb
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

上一篇 下一篇

猜你喜欢

热点阅读