美文网首页iOS点点滴滴
TexturePacker与Xcode集成优化本地图片加载

TexturePacker与Xcode集成优化本地图片加载

作者: lesmiserables0 | 来源:发表于2017-12-27 11:54 被阅读23次
起因
 TexturePacker是一款很好用的图片打包工具,一直以来,都是cocos2dx开发者的钟爱。  
 最近在改游戏项目时接触到了TexturePacker,于是就想把它用到普通OC项目中。
 TexturePacker的使用方法,很简便,自己搜一下,一目了然,这里就不再说了。
 网上查了很多例子,都是用shell脚本集成的,不是很好用。
 于是自己写了一个。记录在这里,以备后用。
功能
 将小图合成一张大图,使用时,按尺寸切除小图,减少调用IO次数,从而是程序更流畅。
TexturePacker打包后的图片
TexturePacker打包后生成的plist文件结构
代码
//从mainBundle中读取图片和plist文件
 UIImage *originalPic = [UIImage imageNamed:@"chatExpression"];
 NSString *path = [[NSBundle mainBundle] pathForResource:@"chatExpression" ofType:@"plist" ];
 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
 NSDictionary *frames = dict[@"frames"];

 NSString *imageName = @"1.png";
 UIImage *image = nil;
 if (frames[imageName]){
      DebugLog(@"stop:%@",imageName);
      image = [self clicpImageFrom:originalPic WithParam:frames[imageName]];
 }else{
      image = nil;
 }

切图方法

-(UIImage *)clicpImageFrom:(UIImage *)originalPic WithParam:(NSDictionary *)parm{
    CGRect rect = [self RectFromString:parm[@"frame"]];
    BOOL rotate = NO;
    if ([parm[@"rotated"]intValue] == 0) {
        rotate = NO;
    }else{
        rotate = YES;
        //如果需要翻转,切图时也要交换宽高。
        CGFloat width = rect.size.width;
        CGFloat height = rect.size.height;
        rect.size.height = width;
        rect.size.width = height;
    }
    UIImage *image = nil;
    CGImageRef cgRef = originalPic.CGImage;
    CGImageRef imageRef = CGImageCreateWithImageInRect(cgRef, rect);
    image = [UIImage imageWithCGImage:imageRef];
    if (rotate){
        image = [UIImage imageWithCGImage:imageRef scale:1 orientation:UIImageOrientationLeft];
    }
    CGImageRelease(imageRef);
    return image;
}

将字符串转换成CGRect结构体方法

-(CGRect)RectFromString:(NSString*)str{
    if (str.length == 0) {
        return CGRectZero;
    }
    CGRect rect = CGRectZero;
    NSArray *strArray = [str componentsSeparatedByString:@","];
    NSMutableArray *floatArray = [NSMutableArray array];
    for (int i = 0 ; i < strArray.count ; i ++) {
        NSString *temp = [strArray[i] stringByReplacingOccurrencesOfString:@"{" withString:@""];
        NSString *temp1 = [temp stringByReplacingOccurrencesOfString:@"}" withString:@""];
        [floatArray addObject:temp1];
    }
    rect.origin.x = [floatArray[0]floatValue];
    rect.origin.y = [floatArray[1]floatValue];
    rect.size.width = [floatArray[2]floatValue];
    rect.size.height = [floatArray[3]floatValue];
    return rect;
}
总结
原本要执行71次IO操作,修改后,IO操作只有一次。软件运行更加流畅。
其实优化工作最主要的是找到卡顿的源头。
//打印关键代码的运行时间,可以判断出卡顿位置
 NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
 //测试代码
 UIImage = image = [UIImage imageNamed:@"1.png"];
 NSDate* dat2 = [NSDate dateWithTimeIntervalSinceNow:0];
 NSTimeInterval time = [dat2 timeIntervalSinceDate:dat];
 NSLog(@"currentTimeString_cha:%f",time);
如有纰漏,请在评论里指出。

如果对你有用,请点喜欢和收藏。

相关文章

网友评论

    本文标题:TexturePacker与Xcode集成优化本地图片加载

    本文链接:https://www.haomeiwen.com/subject/pmxoyxtx.html