工具文策略大本营:自由,平等,友爱。iOS面试专题

iOS经验-优化

2021-03-29  本文已影响0人  iOS开发面试总结

作者:灰太狼同志
链接:https://juejin.cn/post/6916801316433952782

前言:最近应该有很多小伙伴去跳槽面试的吧,相信各位有的已经顺利收到offer了,而有些则是碰壁了,那么我在这里给大家准备了相关面试资料,还有相关算法资料。想了解的可找我拿

卡顿优化

优化的主要思路尽可能减少CPU、GPU资源消耗

CPU优化

// 文字计算
   [@"text" boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];

   // 文字绘制
   [@"text" drawWithRect:CGRectMake(0, 0, 100, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
- (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优化

离屏渲染触发

//生成圆角的图片
- (UIImage *)ht_getCircleImage
{
    //UIGraphicsBeginImageContext(self.size); 相当于(self.size,NO,1) 得到的图片质量差
    UIGraphicsBeginImageContextWithOptions(self.size, YES, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctx, rect);
    CGContextClip(ctx);
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

/// UIImage加圆角
/// @param cornerRadius 圆角
- (UIImage *)ht_getCircleImageWithCornerRadius:(CGFloat)cornerRadius {
    //YES:生成的图片背景不透明  0:表示让图片的缩放因子根据屏幕的分辨率而变化
    UIGraphicsBeginImageContextWithOptions(self.size, YES, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0.0, 0.0, self.size.width, self.size.height);
    CGContextAddPath(ctx, [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius].CGPath);
    CGContextClip(ctx);
    [original drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

耗电优化

启动优化

APP的冷启动分为三大阶段

优化点

减少动态库、合并一些动态库(定期清理不必要的动态库)
减少Objc类、分类的数量、减少Selector数量(定期清理不必要的类、分类)
减少C++虚函数数量
Swift尽量使用struct
用+initialize方法和dispatch_once取代所有的__attribute__((constructor))、C++静态构造器、ObjC的+load
在不影响用户体验的前提下,尽可能将一些操作延迟,不要全部都放在finishLaunching方法中
按需加载

ipa体积优化

文章到这里就结束了,你也可以私信我及时获取最新资料以及面试相关资料。如果你有什么意见和建议欢迎给我留言。

上一篇 下一篇

猜你喜欢

热点阅读