zoukankan      html  css  js  c++  java
  • 关于项目优化的一些小技巧

    1.若项目能编译成功,但有错误提示时,可以用清理缓存的方式解决。

    就是把/Users/用户名/Library/Developer/Xcode/DerivedData文件全部删除。
    但要注意的是删除前要关闭项目,否则该问题仍存在。

     

    2.利用NSData读取文件

    NSData读取文件时分两种形式,通过网络读取和本地读取。二者不能混淆,否则会导致nil问题

    //读取本地文件
    NSData *dataTemp = [NSData dataWithContentsOfFile:@"/Volumes/IMG_992.png"];
    
    //读取网络文件
    NSUrl *url = [NSUrl URLWithString:@"http://baike.baidu.com/pic/%E7%BE%8E%E5%9B%BD/125486/0/377adab44aed2e73523e86b38501a18b86d6fa5e?fr=lemma&ct=single#aid=0&pic=377adab44aed2e73523e86b38501a18b86d6fa5e"];
    NSData *dataTemp = [NSData dataWithContentsOfURL:url];

    若读取本地文件时使用dataWithContentsOfURL,如

    NSUrl *url = [NSUrl URLWithString:@"/Volumes/IMG_992.png"];
    NSData *dataTemp = [NSData dataWithContentsOfURL:url];

    那么dataTemp将会是nil,这二者不能混淆

     

    3.图片优化的一个小技巧

    就是获取图片时,可以用imageWithContentsOfFile来代替[UIImage imageNamed:@""]

    因为imageNamed读取图片时,会缓存在内存中,不容易被释放,所以适合存放一些小图。在使用imageWithContentsOfFile时需注意两个问题
    ①.xcassets里的图片无法被imageWithContentsOfFile读取
    ②imageWithContentsOfFile读取图片需要加图片的扩展名,如png,jpg等

     

    UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 300, 200)];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"];
    imgView1.image = [UIImage imageWithContentsOfFile:filePath];

     

     

    4.在xcode中debug时,若想po self.view.frame或po id类型数据时,会出现

    那么此时就需要在终端输入三条指令使之能打印出来

    1.  touch ~/.lldbinit
    2.  echo display @import UIKit >> ~/.lldbinit
    3.  echo target stop-hook add -o "target stop-hook disable" >> ~/.lldbinit

    输完后若不出现任何提示,则表示操作成功。
    此时重新开始项目即可,无需重启Xcode。成功后

     

     

    5.使用quartz2d画图中带有create,copy,retain等方法创建出来的值都必须手动释放,否则在analyze时会报内存溢出问题。

    手动释放有两种方法

    CGPathRelease(path);
    CFRelease(path);

    CAShapeLayer *layer = [CAShapeLayer new];
    UIBezierPath *path = [UIBezierPath new];
        
    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
    layer.bounds = CGPathGetBoundingBox(bound);

    手动释放该值,必须有

    CGPathRelease(bound);

    或者使用

    CFRelease(bound);
    //CFRelease属于更底层的cocafoundation框架

     

    6.获取文件属性,常用的为[NSFileManager attributesOfItemAtPath:error:],返回类型为NSDictionary

     

    -(void)findFileInfoByFileManager{
        NSFileManager *manager = [NSFileManager defaultManager];
        NSDictionary *dict = [manager attributesOfItemAtPath:_filePath error:nil];
        NSLog(@"%----@",dict);
    }

     

    打印结果为:

    {
        NSFileCreationDate = "2016-05-24 02:55:35 +0000";
        NSFileExtendedAttributes =     {
            "com.apple.quarantine" = <30303032 3b353734 33633261 373b5363 7265656e 43617074 7572653b>;
        };
        NSFileExtensionHidden = 0;
        NSFileGroupOwnerAccountID = 20;
        NSFileGroupOwnerAccountName = staff;
        NSFileModificationDate = "2016-05-24 02:55:35 +0000";
        NSFileOwnerAccountID = 502;
        NSFilePosixPermissions = 420;
        NSFileReferenceCount = 1;
        NSFileSize = 367105;
        NSFileSystemFileNumber = 40523851;
        NSFileSystemNumber = 16777220;
        NSFileType = NSFileTypeRegular;
    }

    但attributesOfItemAtPath:error:会耗费大量时间去读取可能根本不需要使用的属性,这时可以用stat代替NSFileManager。前提是得引入<sys/stat.h>

    #import <sys/stat.h>
    -(void)findFileInfoByStat{
        struct stat statbuf;
        const char *cpath = [_filePath fileSystemRepresentation];
        if (cpath && stat(cpath, &statbuf)==0) {
            NSNumber *fileSize = [NSNumber numberWithUnsignedLongLong:statbuf.st_size];
            NSDate *modifiDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_mtime];
            NSDate *creationDate = [NSDate dateWithTimeIntervalSince1970:statbuf.st_ctime];
            //…………
        }
    }

     

  • 相关阅读:
    08 正则表达式
    07 函数&对象
    06 Math&Date&Json
    05 数组&字符串
    04 循环控制
    03 流程控制
    02 数据类型&运算符
    大道至简
    Avg_row_length是怎么计算的?
    理解innodb buffer pool
  • 原文地址:https://www.cnblogs.com/Apologize/p/5227458.html
Copyright © 2011-2022 走看看