- IOS的一些小知识
-
转载:http://www.2cto.com/kf/201302/188365.html
1.停止UIView动画的方法:
#import<QuartzCore/QuartzCore.h>[self.view.layer removeAllAnimations];2.block页面传值方法:3.UITableViewCell中cell坐标转换cell相对self.view的定位:CGRect rect=[self.view convertRect:cell.frame fromView:tableView];找到cell的相对位置后cell上button获取其他控件的相对位置都可以找出来4.按文件创建时间排序NSLog(@"cheng %@", cheng);NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:@"createDate" ascending:NO];//降序NSMutableArray *sortDescriptors=[[NSMutableArray alloc]initWithObjects:&sorter count:1];NSArray *sortArray=[cheng sortedArrayUsingDescriptors:sortDescriptors];[sorter release];NSLog(@"then %@", sortArray);5.IOS去文件后缀名第一种方法:NSArray *result = [fileName componentsSeparatedByString:@"."];if(result.count != 2){return ;}NSString *fileTitle = [result objectAtIndex:0];NSString *fileType = [result objectAtIndex:1];第二种方法:NSString * fileName;//文件名NSRange range = [sourceFile rangeOfString:@"." options:NSBackwardsSearch];if (range.length>0)fileName=[data.m_strTitle substringToIndex:NSMaxRange(range)];第二种能够排除文件名字中带有.的情况6.UIWebView 隐藏拖拽时上下阴影//隐藏阴影for (UIView *subView in [m_webView subviews]){if ([subView isKindOfClass:[UIScrollView class]]){for (UIView *shadowView in [subView subviews]){if ([shadowView isKindOfClass:[UIImageView class]]){ shadowView.hidden = YES;}}}}7.通过tag取cell上的控件[cell.contentView viewWithTag:tag];8.NSString 编码UTF8//NSString 编码,解决url中空格和汉字NSString * urlString= (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)urlStr, nil, nil,kCFStringEncodingUTF8);9.iOS 获取本地视频的缩略图+(UIImage *)getImage:(NSString *)videoURL{AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];gen.appliesPreferredTrackTransform = YES;CMTime time = CMTimeMakeWithSeconds(0.0, 600);NSError *error = nil;CMTime actualTime;CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];UIImage *thumb = [[UIImage alloc] initWithCGImage:image];CGImageRelease(image);return thumb;}需要添加AVFoundation和CoreMedia.framework10.验证图片有效性- (BOOL)isJPEGValid:(NSData *)jpeg {if ([jpeg length] < 4) return NO;const char * bytes = (const char *)[jpeg bytes];if (bytes[0] != 0xFF || bytes[1] != 0xD8) return NO;if (bytes[[jpeg length] - 2] != 0xFF || bytes[[jpeg length] - 1] != 0xD9) return NO;return YES;}11.UIButton标题对齐方式m_btnCode.titleLabel.textAlignment=UITextAlignmentLeft;//没用需要使用[m_btnCode setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];12.ios 图片保存到相册UIImageWriteToSavedPhotosAlbum是UIKit框架中的一个函数。这里说一下后面三个参数的含义:void UIImageWriteToSavedPhotosAlbum (UIImage *image,id completionTarget,SEL completionSelector,void *contextInfo);id是target对象,sel是selector,即target对象上的方法名,contextInfo是任意指针,会传递到selector定义的方法上。一般是当完成后调用方法时使用,或者在完成时出错的处理。使用的时候:UIImageWriteToSavedPhotosAlbum(m_currentImage.image, nil, nil, nil);//然后在相册中就能找到你添加的图片13.ios 打开照片库,打开相机//打开照片库UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self;//imagePicker.allowsImageEditing = YES; //图片可以编辑//需要添加委托[self presentModalViewController:imagePicker animated:YES];[imagePicker release];//打开相机if([UIImagePickerControlle isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self;//imagePicker.allowsImageEditing = YES;imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;[self presentModalViewController:imagePicker animated:YES];[imagePicker release];}13.CGRectOffset 的作用相对于源矩形原点(左上角的点)沿x轴和y轴偏移 ,例如:[self.view setFrame:CGRectOffset(self.view.frame, 260, 0)];view沿着(0,0)x轴向右移动260个像素14.数组深拷贝NSMutableArray *arr1=[[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil];NSMutableArray *arr2=[[NSMutableArray alloc] init];arr2=[arr1 mutableCopy];[arr1 removeObject:@"b"];//结果arr1:a,c//arr2:a,b,c15.UITableViewCell 重用解决在cell中添加其他控件时,滚动后重复加载,文字重影,控件错乱的情况NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];//添加代码}16.UITextView根据文本修改高度首先根据文本计算高度:UIFont *font = [UIFont systemFontOfSize:14.0];CGSize size = [m_textView.text sizeWithFont:font constrainedToSize:CGSizeMake(240,9999) lineBreakMode:UILineBreakModeWordWrap];//constrainedToSize的两个参数没搞明白,可能第一个参数是textView的宽度,第二个参数还没搞懂然后修改textView的高度[m_textView setFrame:CGRectMake(52, 104, 240, size.height+30)];17.viewDidLoad在view 从nib文件初始化时调用,loadView在controller的view为nil时调用。此方法在编程实现view时调用,view 控制器默认会注册memory warning notification,当view controller的任何view 没有用的时候,viewDidUnload会被调用,在这里实现将retain 的view release,如果是retain的IBOutlet view 属性则不要在这里release,IBOutlet会负责release 。18.delegate和Notification的区别delegate针对one-to-one关系,并且reciever可以返回值给sender;notification 可以针对one-to-one/many/none,reciever无法返回值给sender;所以,delegate用于sender希望接受到reciever的某个功能反馈值,notification用于通知多个object某个事件。19.用变量a给出下面的定义a) 一个整型数(An integer)b) 一个指向整型数的指针(A pointer to an integer)c) 一个指向指针的的指针,它指向的指针是指向一个整型数(A pointer to a pointer to an integer)d) 一个有10个整型数的数组(An array of 10 integers)e) 一个有10个指针的数组,该指针是指向一个整型数的(An array of 10 pointers to integers)f) 一个指向有10个整型数数组的指针(A pointer to an array of 10 integers)g) 一个指向函数的指针,该函数有一个整型参数并返回一个整型数(A pointer to a function that takes an integer as an argument and returns an integer)h) 一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数( An array of ten pointers to functions that take an integerargument and return an integer )(1) int a; // An integer(2) int *a; // A pointer to an integer(3) int **a; // A pointer to a pointer to an integer(4) int a[10]; // An array of 10 integers(5) int *a[10]; // An array of 10 pointers to integers(6) int (*a)[10]; // A pointer to an array of 10 integers(7) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer(8) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer20.NSURL转化NSString 、NSMutableArray转化NSArrayNSURL转化NSStringNSURL *url=····NSString *str=[url absoluteString];NSMutableArray转化NSArray:NSMutableArray *list=····NSArray *list=[list mutableCopy]
;