IOS的一些小知識

來源:互聯網
上載者:User

標籤:

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 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 integer 20.NSURL轉化NSString 、NSMutableArray轉化NSArrayNSURL轉化NSStringNSURL *url=····NSString *str=[url absoluteString]; NSMutableArray轉化NSArray:NSMutableArray *list=····NSArray *list=[list mutableCopy]; 

IOS的一些小知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.