你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS的一些小知識

IOS的一些小知識

編輯:IOS開發綜合
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.framework   10.驗證圖片有效性 - (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,c   15.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 integer argument 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 integer   20.NSURL轉化NSString 、NSMutableArray轉化NSArray NSURL轉化NSString NSURL *url=···· NSString *str=[url absoluteString];   NSMutableArray轉化NSArray: NSMutableArray *list=···· NSArray *list=[list mutableCopy];  
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved