iOS性能优化---转载《三》一次对MKMapView的性能优化(instrments core animation 使用)

本文主要是介绍iOS性能优化---转载《三》一次对MKMapView的性能优化(instrments core animation 使用),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

最近做的项目主要是LBS这块 主打成员定位功能 我们的UI设计是这样的


乍一看上去是挺好挺美观的 不同的人会显示不同的头像 可是当人扎堆的时候 问题就来了


当人多的时候(例如上图所示) 地图滑动起来就能感觉到明显顿卡 那种不流畅感能折磨死人 所以 自然我们要解决这个问题(等等 先不要吐槽为什么不用地图聚合 因为这已经是地图放到最大了 聚合不适合这次的问题讨论)

分析

首先看下我是怎么实现这个annotationView的 由于这个annotationsView是异形的(也就是无法通过设置圆角直接得到) 而且里面的图片还因用户而异 所以解决方案就是使用layer.mask来进行遮罩 代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

@implementation MMAnnotationView

- (instancetype)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    if ( self )

    {

        self.frame = CGRectMake(0, 0, TRACK_ANNOTATION_SIZE.width, TRACK_ANNOTATION_SIZE.height);

        self.centerOffset = CGPointMake(0, -(TRACK_ANNOTATION_SIZE.height-3)/2);

        self.canShowCallout = NO;

        self.avatarView = [[UIImageView alloc] initWithFrame:self.bounds];

        [self addSubview:self.avatarView];

        self.avatarView.contentMode = UIViewContentModeScaleAspectFill;

        CAShapeLayer *shapelayer = [CAShapeLayer layer];

        shapelayer.frame = self.bounds;

        shapelayer.path = self.framePath.CGPath;

        self.avatarView.layer.mask = shapelayer;

        self.layer.shadowPath = self.framePath.CGPath;

        self.layer.shadowRadius = 1.0f;

        self.layer.shadowColor = [UIColor colorWithHex:0x666666FF].CGColor;

        self.layer.shadowOpacity = 1.0f;

        self.layer.shadowOffset = CGSizeMake(0, 0);

        self.layer.masksToBounds = NO;

    }

    return self;

}

//mask路径

- (UIBezierPath *)framePath

{

    if ( !_framePath )

    {

        CGFloat arrowWidth = 14;

        CGMutablePathRef path = CGPathCreateMutable();

        CGRect rectangle = CGRectInset(CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetWidth(self.bounds)), 3,3);

        CGPoint p[3] = {

        {CGRectGetMidX(self.bounds)-arrowWidth/2, CGRectGetWidth(self.bounds)-6},

        {CGRectGetMidX(self.bounds)+arrowWidth/2, CGRectGetWidth(self.bounds)-6},

        {CGRectGetMidX(self.bounds), CGRectGetHeight(self.bounds)-4}

        };

        CGPathAddRoundedRect(path, NULL, rectangle, 5, 5);

        CGPathAddLines(path, NULL, p, 3);

        CGPathCloseSubpath(path);

        _framePath = [UIBezierPath bezierPathWithCGPath:path];

        CGPathRelease(path);

    }

    return _framePath;

}

我用代码生成了形状路径 并以此生成了layer的mask和shadowPath

使用时 只要直接用SDWebImage设置头像就行了

1

[annotationView.avatarView sd_setImageWithURL:[NSURL URLWithString:avatarURL] placeholderImage:placeHolderImage];

接下来用工具分析一下问题出来哪 分析性能当然是选择Instrments(用法在这里就不做介绍了) 打开Core Animation 然后运行程序 滑动地图 可以看到性能分析如下


原来平均帧数只有不到30帧 这离我们的目标60帧差得实在太远

再使用Debug Option来深入分析一下


由于MKMapView的原因 这里我们主要关心这几个选项

  • Color Blended Layers
  • Color Misaligned Images
  • Color Offscreen-Rendered Yellow

分别打开这几个选项 结果如下


可以看到

  • Color Blended Layers没有问题 不过这也是正常的 由于使用了mask 没有透明的地方
  • Color Misaligned Images除了默认头像外全中 这是因为服务器上的图片大小跟显示的大小不一致 导致缩放 而默认头像则是一致的 所以没问题
  • Color Offscreen-Rendered Yellow全中 由于使用了mask 导致大量的离屏渲染 这也是性能下降的主要原因

解决

问题的原因找到了 那么接下来该如何解决呢?

  • 首先mask是肯定不能用了
  • 其次下载下来的图片我们要预处理成实际大小

那么 直接把下载下来的图片合成为我们要显示的最终结果不就ok了吗? 试试看

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

- (void)loadAnnotationImageWithURL:(NSString*)url imageView:(UIImageView*)imageView

{

    //将合成后的图片缓存起来

    NSString *annoImageURL = url;

    NSString *annoImageCacheURL = [annoImageURL stringByAppendingString:@"cache"];

    UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:annoImageCacheURL];

    if ( cacheImage )

    {

        //LLLog(@"hit cache");

        imageView.image = cacheImage;

    }

    else

    {

        //LLLog(@"no cache");

        [imageView sd_setImageWithURL:[NSURL URLWithString:annoImageURL]

        placeholderImage:placeHolderImage

        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        if (!error)

        {

            UIImage *annoImage = [image annotationImage];

            imageView.image = annoImage;

            [[SDImageCache sharedImageCache] storeImage:annoImage forKey:annoImageCacheURL];

            }

        }];

    }

}

@implementation UIImage (LJC)

- (UIImage*) annotationImage

{

    static UIView *snapshotView = nil;

    static UIImageView *imageView = nil;

    if ( !snapshotView )

    {

        snapshotView = [UIView new];

        snapshotView.frame = CGRectMake(0, 0, TRACK_ANNOTATION_SIZE.width, TRACK_ANNOTATION_SIZE.height);

        imageView = [UIImageView new];

        [snapshotView addSubview:imageView];

        imageView.clipsToBounds = YES;

        imageView.frame = snapshotView.bounds;

        imageView.contentMode = UIViewContentModeScaleAspectFill;

        CGFloat arrowWidth = 14;

        CGMutablePathRef path = CGPathCreateMutable();

        CGRect rectangle = CGRectInset(CGRectMake(0, 0, CGRectGetWidth(imageView.bounds), CGRectGetWidth(imageView.bounds)), 3,3);

        CGPoint p[3] = {

            {CGRectGetMidX(imageView.bounds)-arrowWidth/2, CGRectGetWidth(imageView.bounds)-6},

            {CGRectGetMidX(imageView.bounds)+arrowWidth/2, CGRectGetWidth(imageView.bounds)-6},

            {CGRectGetMidX(imageView.bounds), CGRectGetHeight(imageView.bounds)-4}

        };

        CGPathAddRoundedRect(path, NULL, rectangle, 5, 5);

        CGPathAddLines(path, NULL, p, 3);

        CGPathCloseSubpath(path);

        CAShapeLayer *shapelayer = [CAShapeLayer layer];

        shapelayer.frame = imageView.bounds;

        shapelayer.path = path;

        imageView.layer.mask = shapelayer;

        snapshotView.layer.shadowPath = path;

        snapshotView.layer.shadowRadius = 1.0f;

        snapshotView.layer.shadowColor = [UIColor colorWithHex:0x666666FF].CGColor;

        snapshotView.layer.shadowOpacity = 1.0f;

        snapshotView.layer.shadowOffset = CGSizeMake(0, 0);

        CGPathRelease(path);

    }

    imageView.image = self;

    UIGraphicsBeginImageContextWithOptions(TRACK_ANNOTATION_SIZE, NO, 0);

    [snapshotView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *copied = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return copied;

}

@end

然后使用的时候 只要简单的如下调用就OK了

1

[self loadAnnotationImageWithURL:avatarURL imageView:annotationView.avatarView];

看看修改之后的Instruments表现如何


  • Color Blended Layers全中 这也是无可避免的 因为显示的就是一张带透明度的图 但是由于地图的特殊性(头像的位置变化间隔较长 所以不会经常引发合成 也没有动画) 所以这里也不是问题
  • Color Misaligned Images没问题了 因为头像已被缩放成了相同大小
  • Color Offscreen-Rendered Yellow没问题了 因为只是简单的显示了一张图片 而并没有需要离屏渲染的东西了

再来看下帧数情况


Oh-Yeah~ 不光帧数达到了我们的目标60帧(由于还有业务逻辑线程在后台跑 所以没有那么的稳定) 就连平均运行耗时都下降了不少 就算地图上再多显示几十个人 也不成问题了

小结

不光是MKMapView 其实包括UITableView在内的很多地方都可以用文中所说的方法去优化 其核心点就是 合成+缓存 当然 由于合成还是会耗费一部分资源的 所以比较适合头像这种小的资源

关于图形性能优化 可以看下这篇好文(有对文中提到的Debug Option不太明白的 这里有详细的解释)




原文地址:http://www.cocoachina.com/ios/20150717/12583.html

这篇关于iOS性能优化---转载《三》一次对MKMapView的性能优化(instrments core animation 使用)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/405878

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完