iOS 经验总结

1.主线程在动画未结束时进行操作可能导致应用崩溃

如下,当dismissViewControllerAnimated:为YES时,由于后续有页面显示,就在应用中导致了崩溃

- (void)imagePickerController:(UIImagePickerController *)picker

didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

    self.decoding = YES;

    [picker dismissViewControllerAnimated:NO completion:nil];

    

    __block ZXingWidgetController *tempSelf = self;

    tempSelf.imageDecoder = [tempSelf decodeImage:image cropRect:CGRectZero];

}

 2.当发现界面未按代码运行进行刷新时,如tableview reloaddata,界面未刷新,可能是因为reloaddata不是在主线程导致

 

3.简单时间间隔

                static long long timeSince1970 = 0;

                

                long long timeNowSice1970 = [[NSDate date] timeIntervalSince1970];

                //间隔5秒刷新

                if (timeNowSice1970 - timeSince1970 > 5.f) {

                }else {

                }

                timeSince1970 = timeNowSice1970;

4.简单多线程支持

互斥锁使用格式

@synchronized(锁对象) { // 需要锁定的代码  }

 

5.URL中查找字段,CS 需要查找的字段名 

+(NSString *)jiexi:(NSString *)CS webaddress:(NSString *)webaddress

{

    NSError *error;

    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",CS];

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags

                                                                           options:NSRegularExpressionCaseInsensitive

                                                                             error:&error];

    

    // 执行匹配的过程

    // NSString *webaddress=@"http://wgpc.wzsafety.gov.cn/dd/adb.htm?adc=e12&xx=lkw&dalsjd=12";

    NSArray *matches = [regex matchesInString:webaddress

                                      options:0

                                        range:NSMakeRange(0, [webaddress length])];

    for (NSTextCheckingResult *match in matches) {

        //NSRange matchRange = [match range];

        //NSString *tagString = [webaddress substringWithRange:matchRange];  // 整个匹配串

        //        NSRange r1 = [match rangeAtIndex:1];

        //        if (!NSEqualRanges(r1, NSMakeRange(NSNotFound, 0))) {    // 由时分组1可能没有找到相应的匹配,用这种办法来判断

        //            //NSString *tagName = [webaddress substringWithRange:r1];  // 分组1所对应的串

        //            return @"";

        //        }

        

        NSString *tagValue = [webaddress substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串

        //    NSLog(@"分组2所对应的串:%@\n",tagValue);

        return tagValue;

    }

    return @"";

}

- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}

 

6. 日期创建

 

- (NSDate *)checkDate

{

    //2014.23.0.0

    NSDateComponents *components = [[NSDateComponents alloc] init];

    components.timeZone = [NSTimeZone systemTimeZone];

    components.year = 2014;

    components.month = 12;

    components.day = 23;

    components.minute = 0;

    components.second = 0;

    components.hour = 0;

    NSDate *date = [[[NSCalendar currentCalendar] dateFromComponents:components] dateByAddingTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT]];

    return date;

}

 

7、透明的navgationbar

先做一张全通道全透明的图片1*1的像素就行,取名navigation_bar_background.png作为UINavigationBar的背景色,然后讲barStyle设置成通道就可以了。
 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation_bar_background.png"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
 
透明UITabBar同理也可以做成透明。
在iOS 5 以上版本iPad iPhone测试通过。
 
8.Block 返回值

        NSDictionary *(^MenuDicBlock)(enum TReservationStatus state) =/*此处不需要加返回值,由return决定类型*/ ^(enum TReservationStatus state) {

  return @{};

     }

9.arc,非arc公用

#if __has_feature(objc_arc) && __clang_major__ >= 3
#define OBJC_ARC_ENABLED 1
#endif // __has_feature(objc_arc)

#if OBJC_ARC_ENABLED
#define OBJC_RETAIN(object) (object)
#define OBJC_COPY(object) (object)
#define OBJC_RELEASE(object) object = nil
#define OBJC_AUTORELEASE(object) (object)
#else
#define OBJC_RETAIN(object) [object retain]
#define OBJC_COPY(object) [object copy]
#define OBJC_RELEASE(object) [object release], object = nil
#define OBJC_AUTORELEASE(object) [object autorelease]
#endif

 

10 GCD定时器注意事项

如果对定时器用了suspend暂停,那么在销毁定时器前,一定要调用对应次数的resume,否则会导致崩溃。

每一次suspend,resume都将会将内部的一个cout值加减1。

http://stackoverflow.com/questions/9572055/dispatch-source-cancel-on-a-suspended-timer-causes-exc-bad-instruction

 

11.tableview卡顿与ios性能问题

http://blog.sina.com.cn/s/blog_8c08ed5f0102vt0q.html

http://www.keakon.net/2011/07/26/利用预渲染加速iOS设备的图像显示

12.进入当前app权限设置页面

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

posted @ 2014-10-31 17:35  bluefi  阅读(321)  评论(0编辑  收藏  举报