iOS 日常TIPS 收集

圆角图片生成,比较好的姿势:

^UIImage *(UIImage *image) {
            
            UIImage *modifiedImage;
            CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            UIGraphicsBeginImageContextWithOptions(image.size, false, [[UIScreen mainScreen] scale]);
            
            [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:44.0] addClip];
            [image drawInRect:rect];
            modifiedImage = UIGraphicsGetImageFromCurrentImageContext();
            
            UIGraphicsEndImageContext();
            
            return modifiedImage;
            
        };

这里block 的参数 image 是原图,返回的就是一个有圆角的图了,这里圆角度数是 44!!

code 技巧

1:改变UISlider进度条高度重写 trackRectForBounds方法
比如:

- (CGRect)trackRectForBounds:(CGRect)bounds {
    CGRect rect = [super trackRectForBounds:bounds];
    rect.size.height = 4.0f;
    rect.origin.y = (bounds.size.height - rect.size.height) * 0.5;
    return rect;
}

2: 获取宽高尽量使用CGRectGetHeight/CGRectGetWidth

这两个函数返回的总是有效的数值,肯定不会为负数。

3:检查工程中无用的图片资源

使用 LSUnusedResources 检测工程中无用的图片资源。

4:UIWebView 获取 title的方法
[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]

5:图片拉伸处理

- (UIImage *)__stretchImageImageName:(NSString *)name {
    // 加载图片
    UIImage *image = [UIImage imageNamed:name];
    
    // 设置左边端盖宽度
    NSInteger leftCapWidth = image.size.width * 0.5;
    // 设置上边端盖高度
    NSInteger topCapHeight = image.size.height * 0.5;
    
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
    
    return newImage;
}

6: Charles破解

具体见 这里 Charles4.0.2破解.

7:使用NS_DESIGNATED_INITIALIZER指定构造函数

具体见 这里 NS_DESIGNATED_INITIALIZER使用](http://stackoverflow.com/questions/26185239/ios-designated-initializers-using-ns-designated-initializer).

8:通过自定义键盘发出系统键盘的声音

[[UIDevice currentDevice] playInputClick];

9:UIButton设置title 连续闪动解决方案

设置title连续闪动的问题:通常在一个定时器里面去改变一个button的title出出现,解决方案:设置 button的Type 为 Custom


屏幕快照 2017-03-31 上午11.30.33.png

10:xib使用技巧

xib中设置图片后直接command和=号,imageview大小就跟随image了,非常方便。

11:去掉导航栏下方的黑线

[bar setShadowImage:[[UIImage alloc] init]];此处bar 是 UINavigationBar

12:提醒某个方法已经废弃

使用宏__deprecated_msg比如:

@property (nonatomic, assign) BOOL shouldUseCredentialStorage __deprecated_msg("Property deprecated. Does nothing. Kept only for backwards compatibility");

上面的代码是SDWebImage中的代码

13:指定初始化方法

使用宏NS_DESIGNATED_INITIALIZER

14:iOS获取内核数目,以及CPU占有率

+ (NSUInteger)cpuCoreCount
{
    host_basic_info_data_t hostInfo;
    mach_msg_type_number_t infoCount = HOST_BASIC_INFO_COUNT;
    host_info( mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);
    return (NSUInteger)(hostInfo.max_cpus);
}

+ (float)cpuUsage
{
    kern_return_t kr;
    task_info_data_t tinfo;
    mach_msg_type_number_t task_info_count;
    
    task_info_count = TASK_INFO_MAX;
    kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
    if (kr != KERN_SUCCESS) {
        return 0.0f;
    }
    
    task_basic_info_t      basic_info;
    thread_array_t         thread_list;
    mach_msg_type_number_t thread_count;
    
    thread_info_data_t     thinfo;
    mach_msg_type_number_t thread_info_count;
    
    thread_basic_info_t basic_info_th;
    uint32_t stat_thread = 0;
    
    basic_info = (task_basic_info_t)tinfo;
    
    // get threads in the task
    kr = task_threads(mach_task_self(), &thread_list, &thread_count);
    if (kr != KERN_SUCCESS) {
        return 0.0f;
    }
    if (thread_count > 0)
        stat_thread += thread_count;
    
    long tot_sec = 0;
    long tot_usec = 0;
    float tot_cpu = 0;
    int j;
    
    for (j = 0; j < thread_count; j++)
    {
        thread_info_count = THREAD_INFO_MAX;
        kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
                         (thread_info_t)thinfo, &thread_info_count);
        if (kr != KERN_SUCCESS) {
            return 0.0f;
        }
        
        basic_info_th = (thread_basic_info_t)thinfo;
        
        if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
            tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
            tot_usec = tot_usec + basic_info_th->system_time.microseconds + basic_info_th->system_time.microseconds;
            tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
        }
        
    }
    
    kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
    assert(kr == KERN_SUCCESS);
    
    return (tot_cpu > 100.0f) ? 100.f : tot_cpu;
}

15:调用苹果自带的调式界面,添加如下代码即可:
oc代码

Class overlay = NSClassFromString(@"UIDebuggingInformationOverlay");
    [[overlay class] performSelector:NSSelectorFromString(@"prepareDebuggingOverlay")];
    UIWindow *overlayWindow =  (UIWindow *)[[overlay class] performSelector:NSSelectorFromString(@"overlay")];
    [overlayWindow performSelector:NSSelectorFromString(@"toggleVisibility")];
    

16:调试技巧

在Symbolic中填写如下方法签名: -[NSObject(NSObject) doesNotRecognizeSelector:] 快速定位

.unrecognized selector send to instancd
2
1
  1. 当发生crash的时候,我们可以使用thread backtrace查看堆栈调用
  2. 上面的命令等同于bt
  3. Debug的时候,也许会因为各种原因,我们不想让代码执行某个方法,或者要直接返回一个想要的值。这时候就该thread return上场了。
  4. 平时Debug的时候我们经常做的事就是查看变量的值,通过frame variable命令,可以打印出当前frame的所有变量 ,也可以给frame variable传入参数:
  5. 使用watchpoint定位内存释放时刻

17判断当前进程是否正在调试

BOOL isDebuggerPresent(){
    int name[4];                //指定查询信息的数组
    
    struct kinfo_proc info;     //查询的返回结果
    size_t info_size = sizeof(info);
    
    info.kp_proc.p_flag = 0;
    
    name[0] = CTL_KERN;
    name[1] = KERN_PROC;
    name[2] = KERN_PROC_PID;
    name[3] = getpid();
    
    if(sysctl(name, 4, &info, &info_size, NULL, 0) == -1){
        NSLog(@"sysctl error ...");
        return NO;
    }
    
    return ((info.kp_proc.p_flag & P_TRACED) != 0);
}

防止别人调试你的代码

AVFoundition

https://github.com/tapharmonic/Learning-AV-Foundation.git

tips:在文件所在的目录下 python -m SimpleHTTPServer 8000,然后在需要下载的设备上使用浏览器访问http://${ip}:8000 就能看到要传的文件了

你可能感兴趣的:(iOS 日常TIPS 收集)