iOS Coding项目片段记录(六)

//通过图片Data数据第一个字节 来获取图片扩展名   
- (NSString *)contentTypeForImageData:(NSData *)data {   
    uint8_t c;   
    [data getBytes:&c length:1];   
    switch (c) {   
        case 0xFF:   
            return @"jpeg";   
        case 0x89:   
            return @"png";        
        case 0x47:   
            return @"gif";           
        case 0x49:      
        case 0x4D:   
            return @"tiff";           
        case 0x52:     
            if ([data length] < 12) {   
                return nil;   
            }   
            NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];   
            if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {   
                return @"webp";   
            }   
            return nil;   
    }   
    return nil;   
}

 2.设置圆角图片,建设不要使用

 layer.cornerRadius = XX;      layer.masksToBounds = YES;

因为使用图层过量会有卡顿现象, 特别是圆角或者阴影会卡,建议使用如下:

/** 设置圆形图片(放到分类中使用) */   
  - (UIImage *)cutCircleImage {   
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);   
    // 获取上下文   
    CGContextRef ctr = UIGraphicsGetCurrentContext();   
    // 设置圆形   
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);   
    CGContextAddEllipseInRect(ctr, rect);   
    // 裁剪   
    CGContextClip(ctr);   
    // 将图片画上去   
    [self drawInRect:rect];   
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();   
    UIGraphicsEndImageContext();   
    return image;   
}

 3.在iOS中简单的版本号的管理。

首先我们的App第一版本首次上线, 比如以1.0.0为首次上线的版本号:

1、上线后突然发现一个严重的Bug那我们就要修复更新版本, 此时我们的版本号为1.0.1
所以说如果修复Bug或者优化功能, 我们只修改叠加第三位数字, 其他不变

2、如果有了新的需求, 在原来的基础上增加了一个新功能, 那么我们的版本号变为1.1.0, 需要清空第三位数字为0, 来叠加修改第二位数字

3、如果App需求功能大改, 更新量非常大, 那我们的版本号变为2.0.0, 需要叠加修改第一位数字, 清空其他数字为0

 4.使用图片设置tabBar现实的时候会讲图片默认渲染成蓝色,可以通过修改图片显示的渲染模式来取消默认渲染。

方法一:对于图片单独设置渲染模式,如下:
   UIImage *selectedIamge = [UIImage imageNamed:@"tabBar_new_click_icon"];
    
    myVc.tabBarItem.selectedImage = [selectedIamge imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
方法二:在Xcode中设置 Render As 属性如下:

5.统一设置UITabBarItem 的文字属性,获取到Item的 appearance属性设置。

// 通过appearance统一设置所有UITabBarItem的文字属性
 带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

 

posted @ 2016-12-21 14:17  Qingyun_Qearl  阅读(216)  评论(0编辑  收藏  举报