1.压缩图片

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. #pragma mark 处理图片  
  2. - (void)useImage:(UIImage *)image  
  3. {  
  4.     NSLog(@"with-----%f heught-----%f",image.size.width,image.size.height);  
  5.       
  6.     float  scales = image.size.height / image.size.width; //图片比例  
  7.     NSLog(@"图片比例:%f",scales);  
  8.       
  9.     UIImage * normalImg;  
  10.       
  11.     if (image.size.width >300 || image.size.height > 300) {  
  12.           
  13.         if (scales > 1) {  
  14.               
  15.             normalImg = [self imageWithImageSimple:image scaledToSize:CGSizeMake(300 / scales, 300)];  
  16.               
  17.         }else {  
  18.               
  19.             normalImg = [self imageWithImageSimple:image scaledToSize:CGSizeMake(300 ,3300 * scales)];  
  20.         }  
  21.     }  
  22.     else {  
  23.           
  24.         normalImg = image;  
  25.     }  
  26.       
  27.     NSLog(@"第一次处理后:with-----%f height-----%f",normalImg.size.width, normalImg.size.height);  
  28.     CGSize newSize = CGSizeMake(normalImg.size.width, normalImg.size.height);  
  29.     float kk = 1.0f;//图片压缩系数  
  30.     int mm;//压缩后的大小  
  31.     float aa = 1.0f;//图片压缩系数变化步长(可变)  
  32.     mm = (int)UIImageJPEGRepresentation(normalImg, kk).length;  
  33.     while (mm / 1024 > 300) {  
  34.         if (kk > aa + aa / 10) {  
  35.             kk -= aa;  
  36.             if (mm == (int)UIImageJPEGRepresentation(normalImg, kk).length) {  
  37.                 break;  
  38.             }  
  39.             mm = (int)UIImageJPEGRepresentation(normalImg, kk).length;  
  40.         }else{  
  41.             aa /= 10;  
  42.         }  
  43.     }  
  44.     NSLog(@"KK:%f -- aa:%f",kk,aa);  
  45. #warning 图片压缩  
  46.     NSLog(@"第二次处理后:with-----%f height-----%f",normalImg.size.width, normalImg.size.height);  
  47.     NSData *newData;  
  48.     newData = UIImageJPEGRepresentation(normalImg, kk);//最后压缩结果  
  49.       
  50.     if (newData.length/1024 > 300) {  
  51.           
  52.         [APPRequest showAlert:@"提示" message:@"图片过大"];  
  53.          
  54.     }else{  
  55.          
  56.        // 上传图片网络请求  
  57.   
  58.               
  59.             }  
  60.         }];  
  61.     }  
  62. }  

2.发布时间

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. NSDate *  timeDate = [NSDate date];  
  2. NSTimeInterval nowInt = [timeDate timeIntervalSince1970]*1;  
  3. NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];  
  4. [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  
  5. NSDate* publishDate = [dateformatter dateFromString:[demandlist.publish_time substringToIndex:19]];  
  6. NSTimeInterval publishInt = [publishDate timeIntervalSince1970]*1;  
  7. NSTimeInterval cha = nowInt - publishInt;  
  8. NSString *timeString=@"";  
  9.   
  10. if (cha/3600<1) {  
  11.   
  12.     timeString = [NSString stringWithFormat:@"%f", cha/60];  
  13.   
  14.     timeString = [timeString substringToIndex:timeString.length-7];  
  15.   
  16.     timeString = [NSString stringWithFormat:@"%@分钟前发布", timeString];  
  17. }  
  18.   
  19. if (cha/3600>1&&cha/86400<1) {  
  20.   
  21.     timeString = [NSString stringWithFormat:@"%f", cha/3600];  
  22.   
  23.     timeString = [timeString substringToIndex:timeString.length-7];  
  24.   
  25.     timeString = [NSString stringWithFormat:@"%@小时前发布", timeString];  
  26.   
  27. }  
  28.   
  29. if (cha/86400>1)  
  30. {  
  31.   
  32.     timeString = [NSString stringWithFormat:@"%f", cha/86400];  
  33.   
  34.     timeString = [timeString substringToIndex:timeString.length-7];  
  35.   
  36.     timeString = [NSString stringWithFormat:@"%@天前发布", timeString];  
  37.   
  38. }  

3.返回字符串所占的尺寸

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. //返回字符串所占用的尺寸.  
  2. -(CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize  
  3. {    NSDictionary *attrs = @{NSFontAttributeName : font};     
  4.  return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;  
  5. }  

4.tableView自动滑倒某一行

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:10 inSection:0];  
  2. [[self tableView] scrollToRowAtIndexPath:scrollIndexPath  
  3.         atScrollPosition:UITableViewScrollPositionTop animated:YES];  

5.tableView刷新某个分区或某行

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];      
  2. [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];      
  3. //一个cell刷新      
  4. NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];      
  5. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; <span style="font-family: Arial, Helvetica, sans-serif;"> </span>  

6.读取plist文件

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];    
  2.    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];  

7.关键字高亮

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. label.text = [NSString stringWithFormat:@" 视野头条:%@",home.title];  
  2.   
  3. NSString *str = [NSString stringWithFormat:@" 视野头条:%@",home.title];  
  4.   
  5. NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc]initWithString:str];  
  6.   
  7. NSRange range = [str rangeOfString:@"视野头条:"];  
  8.   
  9. [titleStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];  
  10.   
  11. [label setAttributedText:titleStr];  

8.去掉字符串中的空格

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. NSString *str = @"dhak d sh akdl ";  
  2. NSString *strUrl = [str stringByReplacingOccurrencesOfString:@" " withString:@""];  

9.UIImage添加生成圆角图片的扩展API

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. 给UIImage添加生成圆角图片的扩展API:  
  2. - (UIImage *)imageWithCornerRadius:(CGFloat)radius {  
  3.   CGRect rect = (CGRect){0.f, 0.f, self.size};  
  4.   UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);  
  5.   CGContextAddPath(UIGraphicsGetCurrentContext(),  
  6.                    [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);  
  7.   CGContextClip(UIGraphicsGetCurrentContext());  
  8.   [self drawInRect:rect];  
  9.   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  10.   
  11.   UIGraphicsEndImageContext();  
  12.   
  13.   return image;  
  14. }  
  15. //然后调用时就直接传一个圆角来处理:  
  16.   
  17. imgView.image = [[UIImage imageNamed:@"test"] hyb_imageWithCornerRadius:4];  
  18. //最直接的方法就是使用如下属性设置:  
  19. imgView.layer.cornerRadius = 10;  
  20. // 这一行代码是很消耗性能的  
  21. imgView.clipsToBounds = YES;  
  22. // 好处是使用简单,操作方便。坏处是离屏渲染(off-screen-rendering)需要消耗性能。对于图片比较多的视图上,不建议使用这种方法来设 置圆角。通常来说,计算机系统中CPU、GPU、显示器是协同工作的。CPU计算好显示内容提交到GPU,GPU渲染完成后将渲染结果放入帧缓冲区。  
  23. //简单来说,离屏渲染,导致本该GPU干的活,结果交给了CPU来干,而CPU又不擅长GPU干的活,于是拖慢了UI层的FPS(数据帧率),并且离屏需要创建新的缓冲区和上下文切换,因此消耗较大的性能。  

10.正则法则

[html] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. //1.验证邮箱  
  2. + (BOOL)validateEmail:(NSString *)email  
  3. {  
  4.     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";  
  5.     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];  
  6.     return [emailTest evaluateWithObject:email];  
  7. }  
  8. //2.验证手机(简单的)  
  9. + (BOOL)validatePhone:(NSString *)phone  
  10. {  
  11.     NSString *phoneRegex = @"1[3|5|7|8|][0-9]{9}";  
  12.     NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];  
  13.     return [phoneTest evaluateWithObject:phone];  
  14. }  
  15. //验证手机(复杂的)  
  16. + (BOOL)validatePhone:(NSString *)phone  
  17. {  
  18.        /**  
  19.         * 手机号码  
  20.         * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188  
  21.         * 联通:130,131,132,152,155,156,185,186  
  22.         * 电信:133,1349,153,180,189  
  23.         */  
  24.        NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";  
  25.        /**  
  26.         10         * 中国移动:China Mobile  
  27.         11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188  
  28.         12         */  
  29.        NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";  
  30.        /**  
  31.         15         * 中国联通:China Unicom  
  32.         16         * 130,131,132,152,155,156,185,186  
  33.         17         */  
  34.        NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";  
  35.        /**  
  36.         20         * 中国电信:China Telecom  
  37.         21         * 133,1349,153,180,189  
  38.         22         */  
  39.        NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";  
  40.        /**  
  41.         25         * 大陆地区固话及小灵通  
  42.         26         * 区号:010,020,021,022,023,024,025,027,028,029  
  43.         27         * 号码:七位或八位  
  44.         28         */  
  45.       // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";  
  46.   
  47.      NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];  
  48.      NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];  
  49.      NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];  
  50.      NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];  
  51.   
  52.     if (([regextestmobile evaluateWithObject:phone] == YES)  
  53.     || ([regextestcm evaluateWithObject:phone] == YES)  
  54.     || ([regextestct evaluateWithObject:phone] == YES)  
  55.     || ([regextestcu evaluateWithObject:phone] == YES))  
  56.     {  
  57.         if([regextestcm evaluateWithObject:phone] == YES) {  
  58.           NSLog(@"China Mobile");  
  59.         } else if([regextestct evaluateWithObject:phone] == YES) {  
  60.           NSLog(@"China Telecom");  
  61.         } else if ([regextestcu evaluateWithObject:phone] == YES) {  
  62.           NSLog(@"China Unicom");  
  63.         } else {  
  64.           NSLog(@"Unknow");  
  65.         }  
  66.   
  67.         return YES;  
  68.     }  
  69.     else   
  70.     {  
  71.         return NO;  
  72.     }  
  73. }  

11.清除缓存

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. // 删除缓存  
  2. - (void)removeCache  
  3. {  
  4.     NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];  
  5.     NSLog(@"%@",cachePath);  
  6.     NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];  
  7.     for (NSString *p in files) {  
  8.         NSString *path = [NSString stringWithFormat:@"%@/%@", cachePath, p];  
  9.         if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {  
  10.             [[NSFileManager defaultManager] removeItemAtPath:path error:nil];  
  11.         }  
  12.     }  
  13. }  
  14. // 计算清除的缓存大小  
  15. - (CGFloat)floatWithPath:(NSString *)path  
  16. {  
  17.     CGFloat num = 0;  
  18.     NSFileManager *man = [NSFileManager defaultManager];  
  19.     if ([man fileExistsAtPath:path]) {  
  20.         NSEnumerator *childFile = [[man subpathsAtPath:path] objectEnumerator];  
  21.         NSString *fileName;  
  22.         while ((fileName = [childFile nextObject]) != nil) {  
  23.             NSString *fileSub = [path stringByAppendingPathComponent:fileName];  
  24.             num += [self fileSizeAtPath:fileSub];  
  25.         }  
  26.     }  
  27.     return num / (1024.0 * 1024.0);  
  28. }  
  29.   
  30. //计算单个文件大小  
  31. - (long long)fileSizeAtPath:(NSString *)file  
  32. {  
  33.     NSFileManager *man = [NSFileManager defaultManager];  
  34.     if ([man fileExistsAtPath:file]) {  
  35.         return [[man attributesOfItemAtPath:file error:nil] fileSize];  
  36.     }  
  37.     return 0;  
  38. }  

12 系统原生态二维码扫描(包括闪光灯,系统提示音)

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
  1. #import <AVFoundation/AVFoundation.h>  
  2.   
  3. #import <AudioToolbox/AudioToolbox.h> // 系统提示音  
  4. #define SCANVIEW_EdgeTop 40.0  
  5. #define SCANVIEW_EdgeLeft 50.0  
  6. #define TINTCOLOR_ALPHA 0.2 //浅色透明度  
  7. #define DARKCOLOR_ALPHA 0.5 //深色透明度  
  8. #define VIEW_WIDTH [UIScreen mainScreen].bounds.size.width  
  9. #define VIEW_HEIGHT [UIScreen mainScreen].bounds.size.height  
  10. /* 
  11.  *********注意 :系统原生态二维码扫描 苹果官方目前不支持扫扫描图册图片 ************ 
  12.   
  13.  1.第一步 引入框架 AVFoundation.framework 
  14.  2.第二步 声明代理:AVCaptureMetadataOutputObjectsDelegate 。 define 几个东东用来画框、画线: 
  15.   
  16.  
  17.   
  18.  */  
  19. @interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>  
  20. {  
  21.     AVCaptureSession * session;//输入输出的中间桥梁  
  22.     UIView *AVCapView;//此 view 用来放置扫描框、取消按钮、说明 label  
  23.     UIView *_QrCodeline;//上下移动绿色的线条  
  24.     NSTimer *_timer;  
  25. }  
  26. @end  
  27.   
  28. @implementation ViewController  
  29.   
  30. - (void)viewDidLoad {  
  31.     [super viewDidLoad];  
  32.       
  33.     [self createUI];  
  34.     // Do any additional setup after loading the view, typically from a nib.  
  35. }  
  36. #pragma mark 4.在某个方法中(我是点击扫描按钮)创建扫描界面,开始扫描  
  37. - (void)createUI{  
  38.   
  39.     //创建一个 view 来放置扫描区域、说明 label、取消按钮  
  40.     UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height )];  
  41.     AVCapView = tempView;  
  42.     AVCapView.backgroundColor = [UIColor colorWithRed:54.f/255 green:53.f/255 blue:58.f/255 alpha:1];  
  43.       
  44.     UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, [UIScreen mainScreen].bounds.size.height - 100, 50, 25)];  
  45.     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 268, 290, 60)];  
  46.     label.numberOfLines = 0;  
  47.     label.text = @"小提示:将条形码或二维码对准上方区域中心即可";  
  48.     label.textColor = [UIColor grayColor];  
  49.     [cancelBtn setTitle:@"取消" forState: UIControlStateNormal];  
  50.     [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];  
  51.     [cancelBtn addTarget:self action:@selector(touchAVCancelBtn) forControlEvents:UIControlEventTouchUpInside];  
  52.     [AVCapView addSubview:label];  
  53.     [AVCapView addSubview:cancelBtn];  
  54.     [self.view addSubview:AVCapView];  
  55.       
  56.     //画上边框  
  57.     UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH- 22 * SCANVIEW_EdgeLeft, 1)];  
  58.     topView.backgroundColor = [UIColor whiteColor];  
  59.     [AVCapView addSubview:topView];  
  60.       
  61.     //画左边框  
  62.     UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop , 1,VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft )];  
  63.     leftView.backgroundColor = [UIColor whiteColor];  
  64.     [AVCapView addSubview:leftView];  
  65.       
  66.     //画右边框  
  67.     UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft + VIEW_WIDTH- 22 * SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop , 1,VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft + 1)];  
  68.     rightView.backgroundColor = [UIColor whiteColor];  
  69.     [AVCapView addSubview:rightView];  
  70.       
  71.     //画下边框  
  72.     UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop + VIEW_WIDTH- 22 * SCANVIEW_EdgeLeft,VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft ,1 )];  
  73.     downView.backgroundColor = [UIColor whiteColor];  
  74.     [AVCapView addSubview:downView];  
  75.       
  76.     //闪光灯  
  77.     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
  78.     btn.frame = CGRectMake(150, [UIScreen mainScreen].bounds.size.height - 100, 80, 35);  
  79.     [btn setTintColor:[UIColor grayColor]];  
  80.       
  81.     [btn setTitle:@"闪光灯" forState:UIControlStateNormal];  
  82.     [btn addTarget:self action: @selector(OPEN:) forControlEvents:UIControlEventTouchUpInside];  
  83.     [AVCapView  addSubview:btn];  
  84.      
  85.       
  86.     //画中间的基准线  
  87.     _QrCodeline = [[UIView alloc] initWithFrame:CGRectMake(SCANVIEW_EdgeLeft + 1, SCANVIEW_EdgeTop, VIEW_WIDTH- 22 * SCANVIEW_EdgeLeft - 1, 2)];  
  88.     _QrCodeline.backgroundColor = [UIColor greenColor];  
  89.     [AVCapView addSubview:_QrCodeline];  
  90.       
  91.       
  92.     // 先让基准线运动一次,避免定时器的时差  
  93.     [UIView animateWithDuration:1.2 animations:^{  
  94.           
  95.         _QrCodeline.frame = CGRectMake(SCANVIEW_EdgeLeft + 1, VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop , VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft - 1, 2);  
  96.           
  97.     }];  
  98.       
  99.     [self performSelector:@selector(createTimer) withObject:nil afterDelay:0.4];  
  100.       
  101.       
  102.     AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
  103.     //创建输入流  
  104.     AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];  
  105.     //创建输出流  
  106.     AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];  
  107.     //设置代理 在主线程里刷新  
  108.     [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];  
  109.       
  110.     //初始化链接对象  
  111.     session = [[AVCaptureSession alloc]init];  
  112.     //高质量采集率  
  113.     [session setSessionPreset:AVCaptureSessionPresetHigh];  
  114.       
  115.     [session addInput:input];  
  116.     [session addOutput:output];  
  117.     //设置扫码支持的编码格式(如下设置条形码和二维码兼容)  
  118.     output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];  
  119.       
  120.     AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];  
  121.     layer.videoGravity=AVLayerVideoGravityResizeAspectFill;  
  122.     layer.frame = CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH- 22 * SCANVIEW_EdgeLeft, 220);  
  123.     [AVCapView.layer insertSublayer:layer atIndex:0];  
  124.     //开始捕获  
  125.     [session startRunning];  
  126.   
  127. }  
  128. #pragma mark 调用闪光灯  
  129. - (void)OPEN:(UIButton *)btn{  
  130.   
  131.     btn.selected = !btn.isSelected;  
  132.       
  133.     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
  134.       
  135.     if ([device hasTorch]) {  
  136.           
  137.         [device lockForConfiguration:nil];  
  138.           
  139.     if (btn.selected) {  
  140.           
  141.         [device setTorchMode:AVCaptureTorchModeOn];  
  142.           
  143.     }else{  
  144.       
  145.           [device setTorchMode:AVCaptureTorchModeOff];  
  146.     }  
  147.            [device unlockForConfiguration];  
  148.   
  149.     }  
  150. }  
  151. - (void)touchAVCancelBtn{  
  152.   
  153.     //取消按钮的响应时间  
  154. }  
  155. #pragma mark 5.实现定时器、还有基准线的滚动方法  
  156. - (void)createTimer  
  157. {  
  158.     _timer=[NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector(moveUpAndDownLine) userInfo:nil repeats:YES];  
  159. }  
  160.   
  161. - (void)stopTimer  
  162. {  
  163.     if ([_timer isValid] == YES) {  
  164.         [_timer invalidate];  
  165.         _timer = nil;  
  166.     }  
  167.       
  168. }  
  169. // 滚来滚去 :D :D :D  
  170. - (void)moveUpAndDownLine  
  171. {  
  172.     CGFloat YY = _QrCodeline.frame.origin.y;  
  173.       
  174.     if (YY != VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop ) {  
  175.         [UIView animateWithDuration:1.2 animations:^{  
  176.             _QrCodeline.frame = CGRectMake(SCANVIEW_EdgeLeft + 1, VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop , VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft - 1,2);  
  177.         }];  
  178.     }else {  
  179.         [UIView animateWithDuration:1.2 animations:^{  
  180.             _QrCodeline.frame = CGRectMake(SCANVIEW_EdgeLeft + 1, SCANVIEW_EdgeTop, VIEW_WIDTH - 22 * SCANVIEW_EdgeLeft - 1,2);  
  181.         }];  
  182.           
  183.     }  
  184. }  
  185. #pragma mark 6.扫描成功后,想干嘛干嘛,就在这个代理方法里面实现就行了  
  186.   
  187. -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{  
  188.     if (metadataObjects.count>0) {  
  189.         //[session stopRunning];  
  190.         AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];  
  191.         //输出扫描字符串  
  192.         NSLog(@"%@",metadataObject.stringValue);  
  193.         AudioServicesPlaySystemSound(1307);  
  194.         [session stopRunning];  
  195.         [self stopTimer];  
  196.         //[AVCapView removeFromSuperview];  
  197.         
  198.     }  
  199. }  

 

13.webView计算高度

[objc] view plain copy iOS开发中一些常用的方法_icoiOS开发中一些常用的方法_lua_02
    1. //第一种:  
    2.   
    3. - (void)webViewDidFinishLoad:(UIWebView *)webView{  
    4.   
    5. float height = [[webView stringByEvaluatingJavaScriptFromString:@document.body.offsetHeight;] floatValue];  
    6.   
    7. //document.body.scrollHeight  
    8.   
    9. }  
    10.   
    11.    
    12.   
    13. //第二种:  
    14.   
    15.    
    16.   
    17. - (void) webViewDidFinishLoad:(UIWebView *)webView  
    18.   
    19. {  
    20.   
    21. CGRect frame = webView.frame;  
    22.   
    23. CGSize fittingSize = [webView sizeThatFits:CGSizeZero];  
    24.   
    25. frame.size = fittingSize;  
    26.   
    27. webView.frame = frame;  
    28.   
    29. }  
    30.   
    31.    
    32.   
    33. //另外一种  
    34.   
    35. - (void)viewDidLoad {  
    36.   
    37. [super viewDidLoad];  
    38.   
    39. webview.delegate = self;  
    40.   
    41. [webview loadHTMLString:@  
    42.   
    43. fdasfda  
    44.   
    45. baseURL:nil];  
    46.   
    47. }  
    48.   
    49. - (void)webViewDidFinishLoad:(UIWebView *)webView  
    50.   
    51. {  
    52.   
    53. NSString *output = [webview stringByEvaluatingJavaScriptFromString:@document.getElementByIdx_x_x_x(foo).offsetHeight;];  
    54.   
    55. NSLog(@height: %@, output);  
    56.