iOS开发中常用函数及控件

来源:互联网 发布:淘宝的蔬菜种子靠谱吗 编辑:程序博客网 时间:2024/05/22 13:23
  1. #include  /* 说明 malloc, NULL, size_t */
  2. #include  /* 说明 va_ 相关类型和函数 */
  3. #include  /* 说明 strcat 等 */
  4. char *vstrcat(const char *first, ...)
  5. {
  6. size_t len;
  7. char *retbuf;
  8. va_list argp;
  9. char *p;
  10. if(first == NULL)
  11. return NULL;
  12. len = strlen(first);
  13. va_start(argp, first);
  14. while((p = va_arg(argp, char *)) != NULL)
  15. len += strlen(p);
  16. va_end(argp);
  17. retbuf = malloc(len + 1); /* +1 包含终止符 \0 */
  18. if(retbuf == NULL)
  19. return NULL; /* 出错 */
  20. (void)strcpy(retbuf, first);
  21. va_start(argp, first); /* 重新开始扫描 */
  22. while((p = va_arg(argp, char *)) != NULL)
  23. (void)strcat(retbuf, p);
  24. va_end(argp);
  25. retbuf = malloc(len + 1); /* +1 包含终止符 \0 */
  26. if(retbuf == NULL)
  27. return NULL; /* 出错 */
  28. (void)strcpy(retbuf, first);
  29. va_start(argp, first); /* 重新开始扫描 */
  30. while((p = va_arg(argp, char *)) != NULL)
  31. (void)strcat(retbuf, p);
  32. va_end(argp);
  33. return retbuf;
  34. }

  35. %c 一个单一的字符
  36. %d 一个十进制整数
  37. %i 一个整数
  38. %e, %f, %g 一个浮点数
  39. %o 一个八进制数
  40. %s 一个字符串
  41. %x 一个十六进制数
  42. %p 一个指针
  43. %n 一个等于读取字符数量的整数
  44. %u 一个无符号整数
  45. %[] 一个字符集
  46. %% 一个精度符号 

  47. //一、NSString
  48.     /*----------------创建字符串的方法----------------*/

  49.     1、创建常量字符串。
  50.     NSString *astring = @"This is a String!";

  51.     2、创建空字符串,给予赋值。
  52.     NSString *astring = [[NSString alloc] init];
  53.     astring = @"This is a String!";
  54.     NSLog(@"astring:%@",astring);
  55.     [astring release];

  56.     3、在以上方法中,提升速度:initWithString方法
  57.     NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
  58.     NSLog(@"astring:%@",astring);
  59.     [astring release];

  60.     4、用标准c创建字符串:initWithCString方法
  61.     char *Cstring = "This is a String!";
  62.     NSString *astring = [[NSString alloc] initWithCString:Cstring];
  63.     NSLog(@"astring:%@",astring);
  64.     [astring release];

  65.     5、创建格式化字符串:占位符(由一个%加一个字符组成)
  66.     int i = 1;
  67.     int j = 2;
  68.     NSString *astring = [[NSString alloc]
  69.                         initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
  70.     NSLog(@"astring:%@",astring);
  71.     [astring release];

  72.     6、创建临时字符串
  73.     NSString *astring;
  74.     astring = [NSString stringWithCString:"This is a temporary string"];
  75.     NSLog(@"astring:%@",astring);

  76.     /*----------------从文件读取字符串:initWithContentsOfFile方法 ----------------*/
  77.     NSString *path = @"astring.text";
  78.     NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
  79.     NSLog(@"astring:%@",astring);
  80.     [astring release];

  81.     /*----------------写字符串到文件:writeToFile方法 ----------------*/
  82.     NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
  83.     NSLog(@"astring:%@",astring);
  84.     NSString *path = @"astring.text";
  85.     [astring writeToFile: path atomically: YES];
  86.     [astring release];    

  87.     /*---------------- 比较两个字符串----------------*/
  88.     用C比较:strcmp函数
  89.     char string1[] = "string!";
  90.     char string2[] = "string!";
  91.     if(strcmp(string1, string2) = = 0)
  92.     {
  93.         NSLog(@"1");
  94.     }

  95.     isEqualToString方法
  96.     NSString *astring01 = @"This is a String!";
  97.     NSString *astring02 = @"This is a String!";
  98.     BOOL result = [astring01 isEqualToString:astring02];
  99.     NSLog(@"result:%d",result);

  100.     compare方法(comparer返回的三种值)
  101.     NSString *astring01 = @"This is a String!";
  102.     NSString *astring02 = @"This is a String!";
  103.     BOOL result = [astring01 compare:astring02] = = NSOrderedSame;
  104.     NSLog(@"result:%d",result);
  105.     NSOrderedSame 判断两者内容是否相同

  106.     NSString *astring01 = @"This is a String!";
  107.     NSString *astring02 = @"this is a String!";
  108.     BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;
  109.     NSLog(@"result:%d",result);
  110.     //NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)

  111.     NSString *astring01 = @"this is a String!";
  112.     NSString *astring02 = @"This is a String!";
  113.     BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;
  114.     NSLog(@"result:%d",result);
  115.     //NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)

  116.     不考虑大 小写比较字符串1
  117.     NSString *astring01 = @"this is a String!";
  118.     NSString *astring02 = @"This is a String!";
  119.     BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
  120.     NSLog(@"result:%d",result);
  121.     //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为 真)

  122.     不考虑大小写比较字符串2
  123.     NSString *astring01 = @"this is a String!";
  124.     NSString *astring02 = @"This is a String!";
  125.     BOOL result = [astring01 compare:astring02
  126.                             options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;
  127.     NSLog(@"result:%d",result);     

  128.     //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。

  129.     /*----------------改变字符串的大小写----------------*/
  130.     NSString *string1 = @"A String";
  131.     NSString *string2 = @"String";
  132.     NSLog(@"string1:%@",[string1 uppercaseString]);//大写
  133.     NSLog(@"string2:%@",[string2 lowercaseString]);//小写
  134.     NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小

  135.     /*----------------在串中搜索子串 ----------------*/
  136.     NSString *string1 = @"This is a string";
  137.     NSString *string2 = @"string";
  138.     NSRange range = [string1 rangeOfString:string2];
  139.     int location = range.location;
  140.     int leight = range.length;
  141.     NSString *astring = [[NSString alloc]
  142.                         initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i"
  143.                         ,location,leight]];
  144.     NSLog(@"astring:%@",astring);
  145.     [astring release];

  146.     /*----------------抽取子串 ----------------*/
  147.     -substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
  148.     NSString *string1 = @"This is a string";
  149.     NSString *string2 = [string1 substringToIndex:3];
  150.     NSLog(@"string2:%@",string2);

  151.     -substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
  152.     NSString *string1 = @"This is a string";
  153.     NSString *string2 = [string1 substringFromIndex:3];
  154.     NSLog(@"string2:%@",string2);

  155.     -substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
  156.     NSString *string1 = @"This is a string";
  157.     NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
  158.     NSLog(@"string2:%@",string2); 

  159. const char *fieldValue = [value  cStringUsingEncoding:NSUTF8StringEncoding];
  160. const char *fieldValue = [value UTF8String];

  161. NSString 转 NSData
  162. NSString* str= @"kilonet";
  163. NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding]; 

  164.    Date format用法:
  165.   -(NSString *) getDay:(NSDate *) d
  166. {
  167.     NSString *s ;
  168.     NSDateFormatter *format = [[NSDateFormatter alloc] init];
  169.     [format setDateFormat:@"YYYY/MM/dd hh:mm:ss"];
  170.     s = [format stringFromDate:d];
  171.     [format release];
  172.     return s;
  173. }

  174. 各地时区获取:

  175. NSDate *nowDate = [NSDate new];
  176.     NSDateFormatter *formatter    =  [[NSDateFormatter alloc] init];
  177.     [formatter    setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
  178.     //    根据时区名字获取当前时间,如果该时区不存在,默认获取系统当前时区的时间
  179.     //    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Europe/Andorra"];
  180.     //    [formatter setTimeZone:timeZone];
  181.     //获取所有的时区名字
  182.     NSArray *array = [NSTimeZone knownTimeZoneNames];
  183.     //    NSLog(@"array:%@",array);
  184.     //for循环
  185.     //    for(int i=0;i<[array count];i++)
  186.     //    {
  187.     //        NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:[array objectAtIndex:i]];
  188.     //        [formatter setTimeZone:timeZone];
  189.     //        NSString *locationTime = [formatter stringFromDate:nowDate];
  190.     //        NSLog(@"时区名字:%@   : 时区当前时间: %@",[array objectAtIndex:i],locationTime);
  191.     //        //NSLog(@"timezone name is:%@",[array objectAtIndex:i]);
  192.     //    }
  193.     //快速枚举法
  194.     for(NSString *timeZoneName in array){
  195.         [formatter setTimeZone:[NSTimeZone timeZoneWithName:timeZoneName]];
  196.         NSLog(@"%@,%@",timeZoneName,[formatter stringFromDate:nowDate]);
  197.     }

  198.     [formatter release];
  199.     [nowDate release];

  200. NSCalendar用法:

  201. -(NSString *) getWeek:(NSDate *) d {
  202.     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  203.     unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSWeekdayCalendarUnit;
  204.     NSDateComponents *components = [calendar components:units fromDate:d];
  205.     [calendar release];

  206.     switch ([components weekday]) {
  207.         case 2:
  208.             return @"Monday";
  209.             break;
  210.         case 3:
  211.             return @"Tuesday";
  212.             break;
  213.         case 4:
  214.             return @"Wednesday";
  215.             break;
  216.         case 5:
  217.            return @"Thursday";
  218.             break;
  219.         case 6:
  220.             return  @"Friday";
  221.             break;
  222.         case 7:
  223.             return  @"Saturday";
  224.             break;
  225.         case 1:
  226.             return @"Sunday";
  227.             break;
  228.         default:
  229.             return @"No Week";
  230.             break;
  231.     }

  232.     // 用components,我们可以读取其他更多的数据。

  233. }

  234. 4. 用Get方式读取网络数据:

  235. 将网络数读取为字符串
  236. - (NSString *) getDataByURL:(NSString *) url {
  237.     return [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] encoding:NSUTF8StringEncoding];
  238. }

  239. //读取网络图片
  240. - (UIImage *) getImageByURL:(NSString *) url {
  241.     return [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
  242. }

  243. 多线程
  244. [NSThread detachNewThreadSelector:@selector(scheduleTask) toTarget:self withObject:nil];

  245. -(void) scheduleTask {
  246.     //create a pool
  247.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  248.     //release the pool;
  249.     [pool release];
  250. }

  251. //如果有参数,则这么使用:
  252. [NSThread detachNewThreadSelector:@selector(scheduleTask:) toTarget:self withObject:[NSDate date]];

  253. -(void) scheduleTask:(NSDate *) mdate {
  254.     //create a pool
  255.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  256.     //release the pool;
  257.     [pool release];
  258. }

  259. //注意selector里有冒号。
  260.     //在线程里运行主线程里的方法 

  261.     [self performSelectorOnMainThread:@selector(moveToMain) withObject:nil waitUntilDone:FALSE];

  262. 6. 定时器NSTimer用法:

  263. 代码
  264.   // 一个可以自动关闭的Alert窗口

  265.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
  266.                                                     message:[@"一个可以自动关闭的Alert窗口"
  267.                                                    delegate:nil
  268.                                           cancelButtonTitle:nil //NSLocalizedString(@"OK", @"OK")   //取消任何按钮
  269.                                           otherButtonTitles:nil];
  270.     //[alert setBounds:CGRectMake
  271.       (alert.bounds.origin.x, alert.bounds.origin.y,
  272.       alert.bounds.size.width, alert.bounds.size.height+30.0)];
  273.     [alert show];

  274.     UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

  275.     // Adjust the indicator so it is up a few pixels from the bottom of the alert
  276.     indicator.center = CGPointMake(alert.bounds.size.width/2,  alert.bounds.size.height-40.0);
  277.     [indicator startAnimating];
  278.     [alert insertSubview:indicator atIndex:0];
  279.     [indicator release];

  280.     [NSTimer scheduledTimerWithTimeInterval:3.0f
  281.                                      target:self
  282.                                    selector:@selector(dismissAlert:)
  283.                                    userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,
  284.                    @"alert", @"testing ", @"key" ,nil]  //如果不用传递参数,那么可以将此项设置为nil.
  285.                                     repeats:NO];

  286.     NSLog(@"release alert");
  287.     [alert release];

  288. -(void) dismissAlert:(NSTimer *)timer{

  289.     NSLog(@"release timer");
  290.     NSLog([[timer userInfo]  objectForKey:@"key"]);

  291.     UIAlertView *alert = [[timer userInfo]  objectForKey:@"alert"];
  292.     [alert dismissWithClickedButtonIndex:0 animated:YES];

  293. }

  294. 定时器停止使用:

  295. [timer invalidate];
  296. timer = nil;

  297.      7. 用户缺省值NSUserDefaults读取:

  298.     //得到用户缺省值
  299.     NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

  300.     //在缺省值中找到AppleLanguages, 返回值是一个数组
  301.     NSArray* languages = [defs objectForKey:@"AppleLanguages"];
  302.     NSLog(@"all language语言 is %@", languages);

  303.     //在得到的数组中的第一个项就是用户的首选语言了
  304.     NSLog(@"首选语言 is %@",[languages objectAtIndex:0]);  

  305.     //get the language & country code
  306.     NSLocale *currentLocale = [NSLocale currentLocale];

  307.     NSLog(@"Language Code is %@", [currentLocale objectForKey:NSLocaleLanguageCode]);
  308.     NSLog(@"Country Code is %@", [currentLocale objectForKey:NSLocaleCountryCode

  309. 8. View之间切换的动态效果设置:

  310.     SettingsController *settings = [[SettingsController alloc]initWithNibName:@"SettingsView" bundle:nil];
  311.     settings.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;  //水平翻转
  312.     [self presentModalViewController:settings animated:YES];
  313.     [settings release];

  314. 9.NSScrollView 滑动用法:

  315. -(void) scrollViewDidScroll:(UIScrollView *)scrollView{
  316.     NSLog(@"正在滑动中...");
  317. }

  318. //用户直接滑动NSScrollView,可以看到滑动条
  319. -(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

  320. }

  321. // 通过其他控件触发NSScrollView滑动,看不到滑动条
  322. - (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

  323. }

  324.     11.键盘处理系列

  325. //set the UIKeyboard to switch to a different text field when you press return

  326. //switch textField to the name of your textfield
  327. [textField becomeFirstResponder];

  328. srandom(time(NULL)); //随机数种子

  329. id d = random(); // 随机数

  330.    4. iPhone的系统目录:

  331. //得到Document目录:
  332. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  333. NSString *documentsDirectory = [paths objectAtIndex:0];

  334. //得到temp临时目录:
  335. NSString *tempPath = NSTemporaryDirectory();

  336. //得到目录上的文件地址:
  337. NSString *文件地址 = [目录地址 stringByAppendingPathComponent:@"文件名.扩展名"];

  338. 5. 状态栏显示Indicator:

  339. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

  340.   6.app Icon显示数字:

  341. - (void)applicationDidEnterBackground:(UIApplication *)application{
  342.     [[UIApplication sharedApplication] setApplicationIconBadgeNumber:5];
  343. }

  344.    7.sqlite保存地址: 

  345. 代码
  346.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  347.     NSString *thePath = [paths objectAtIndex:0];
  348.     NSString *filePath = [thePath stringByAppendingPathComponent:@"kilonet1.sqlite"];

  349.     NSString *dbPath = [[[NSBundle mainBundle] resourcePath]
  350.                         stringByAppendingPathComponent:@"kilonet2.sqlite"]; 

  351.    8.Application退出:exit(0);

  352.       9. AlertView,ActionSheet的cancelButton点击事件:

  353. 代码
  354. -(void) actionSheet :(UIActionSheet *) actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
  355.     NSLog(@"cancel actionSheet........");
  356.     //当用户按下cancel按钮
  357.     if( buttonIndex == [actionSheet cancelButtonIndex]) {
  358.         exit(0);
  359.     }
  360. //    //当用户按下destructive按钮
  361. //    if( buttonIndex == [actionSheet destructiveButtonIndex]) {
  362. //        // DoSomething here.
  363. //    }
  364. }

  365. - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
  366.      NSLog(@"cancel alertView........");
  367.     if (buttonIndex == [alertView cancelButtonIndex]) {
  368.         exit(0);
  369.     }
  370. }

  371.   10.给Window设置全局的背景图片:
  372. window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"coolblack.png"]];

  373.     11. UITextField文本框显示及对键盘的控制:

  374. 代码
  375. #pragma mark -
  376. #pragma mark UITextFieldDelegate
  377. //控制键盘跳转
  378. - (BOOL)textFieldShouldReturn:(UITextField *)textField {

  379.     if (textField == _txtAccount) {
  380.         if ([_txtAccount.text length]==0) {
  381.             return NO;
  382.         }
  383.         [_txtPassword becomeFirstResponder];
  384.     } else if (textField == _txtPassword) {
  385.         [_txtPassword resignFirstResponder];
  386.     }

  387.     return YES;
  388. }

  389. //输入框背景更换
  390. -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{

  391.     [textField setBackground:[UIImage imageNamed:@"ctext_field_02.png"]];

  392.     return YES;
  393. }

  394. -(void) textFieldDidEndEditing:(UITextField *)textField{
  395.     [textField setBackground:[UIImage imageNamed:@"ctext_field_01.png"]];
  396. }

  397. 12.UITextField文本框前面空白宽度设置以及后面组合按钮设置:

  398. 代码
  399.     //给文本输入框后面加入空白
  400.     _txtAccount.rightView = _btnDropDown;
  401.     _txtAccount.rightViewMode =  UITextFieldViewModeAlways;

  402.     //给文本输入框前面加入空白
  403.     CGRect frame = [_txtAccount frame];
  404.     frame.size.width = 5;
  405.     UIView *leftview = [[UIView alloc] initWithFrame:frame];
  406.     _txtAccount.leftViewMode = UITextFieldViewModeAlways;
  407.     _txtAccount.leftView = leftview;

  408.   13. UIScrollView 设置滑动不超出本身范围:

  409. [fcScrollView setBounces:NO]; 

  410. 14. 在drawRect里画文字:

  411.      UIFont * f = [UIFont systemFontOfSize:20]; 

  412.     [[UIColor darkGrayColor] set]; 

  413.     NSString * text = @"hi \nKiloNet"; 

  414.     [text drawAtPoint:CGPointMake(center.x,center.y) withFont:f];

  415.     15. NSArray查找是否存在对象时用indexOfObject,如果不存在则返回为NSNotFound.

  416.     16. NString与NSArray之间相互转换:

  417. array = [string componentsSeparatedByString:@","];
  418. string = [[array valueForKey:@"description"] componentsJoinedByString:@","];

  419.      17. TabController随意切换tab bar:

  420. [self.tabBarController setSelectedIndex:tabIndex];

  421. 或者 self.tabBarController.selectedIndex = tabIndex;

  422. 或者实现下面的delegate来扑捉tab bar的事件: 

  423. 代码-(BOOL) tabBarController:(UITabBarController *)tabBarController
  424. shouldSelectViewController:(UIViewController *)viewController
  425. {        if ([viewController.tabBarItem.title isEqualToString: NSLocalizedString(@"Logout",nil)])
  426. {        [self showLogout];        return NO;    }    return YES;}

  427.     18. 自定义View之间切换动画:
  428. 代码
  429. - (void) pushController: (UIViewController*) controller
  430.          withTransition: (UIViewAnimationTransition) transition
  431. {
  432.     [UIView beginAnimations:nil context:NULL];
  433.     [self pushViewController:controller animated:NO];
  434.     [UIView setAnimationDuration:.5];
  435.     [UIView setAnimationBeginsFromCurrentState:YES];
  436.     [UIView setAnimationTransition:transition forView:self.view cache:YES];
  437.     [UIView commitAnimations];
  438. }

  439. CATransition *transition = [CATransition animation];
  440. transition.duration = kAnimationDuration;
  441. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  442. transition.type = kCATransitionPush;
  443. transition.subtype = kCATransitionFromTop;
  444. transitioning = YES;
  445. transition.delegate = self;
  446. [self.navigationController.view.layer addAnimation:transition forKey:nil];

  447. self.navigationController.navigationBarHidden = NO;
  448. [self.navigationController pushViewController:tableViewController animated:YES];

  449.      20.计算字符串长度:

  450. CGFloat w = [title sizeWithFont:[UIFont fontWithName:@"Arial" size:18]].width; 

  451.   23.在使用UISearchBar时,将背景色设定为clearColor,或者将translucent设为YES,都不能使背景透明,经过一番研究,发现了一种超级简单和实用的方法:

  452. 1
  453. [[searchbar.subviews objectAtIndex:0]removeFromSuperview]; 

  454. 背景完全消除了,只剩下搜索框本身了。 

  455.   24.  图像与缓存 :

  456. UIImageView *wallpaper = [[UIImageView alloc] initWithImage:

  457.         [UIImage imageNamed:@"icon.png"]]; // 会缓存图片

  458. UIImageView *wallpaper = [[UIImageView alloc] initWithImage:

  459.         [UIImage imageWithContentsOfFile:@"icon.png"]]; // 不会缓存图片 

  460.   25. iphone-常用的对视图图层(layer)的操作

  461. 对图层的操作:

  462. (1.给图层添加背景图片:
  463. myView.layer.contents = (id)[UIImage imageNamed:@"view_BG.png"].CGImage;

  464. (2.将图层的边框设置为圆脚
  465. myWebView.layer.cornerRadius = 8;
  466. myWebView.layer.masksToBounds = YES;

  467. (3.给图层添加一个有色边框
  468. myWebView.layer.borderWidth = 5;
  469. myWebView.layer.borderColor = [[UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1] CGColor];
复制代码
0 0