性能优化

面试题

1.你在项目里面是怎么优化内存的?
2.优化你是从哪几个方面入手?
3.列表卡顿的原因可能有哪些?平时是怎么优化的?
4.遇到tableview卡顿?会造成卡顿的原因有哪些?

CPU和GPU

PerformanceOptimization_1.png

屏幕成像原理

PerformanceOptimization_2.png

卡顿产生的原因

PerformanceOptimization_3.png

卡顿优化 - CPU

PerformanceOptimization_4.png
图片处理:
- (void)image{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(100, 100, 100, 56);
    [self.view addSubview:imageView];
    self.imageView = imageView;

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 获取CGImage
        CGImageRef cgImage = [UIImage imageNamed:@"timg"].CGImage;

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

        // bitmapInfo
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host;
        bitmapInfo |= hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst;

        // size
        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];

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

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


卡顿优化 - GPU

PerformanceOptimization_5.png

离屏渲染

PerformanceOptimization_6.png

卡顿检测

PerformanceOptimization_7.png

如: LXDAppFluecyMonitor


耗电的主要来源及优化

PerformanceOptimization_8.png
PerformanceOptimization_9.png
PerformanceOptimization_10.png

APP的启动优化

DYLD_PRINT_STATISTICS
DYLD_PRINT_STATISTICS_DETAILS (打印的更加详细)
一般在400ms内属于正常
  • App的启动
PerformanceOptimization_11.png
PerformanceOptimization_12.png
  • App的启动 - dyld
PerformanceOptimization_13.png
  • App的启动 - runtime
PerformanceOptimization_14.png
  • App的启动 - main
PerformanceOptimization_15.png
  • App的启动优化
PerformanceOptimization_16.png

安装包瘦身

PerformanceOptimization_17.png

LSUnusedResources

https://www.jetbrains.com/objc/


LinkMap

PerformanceOptimization_18.png

LinkMap

你可能感兴趣的:(性能优化)