iPhone应用开发学习笔记


  本文标签:iPhone 文件 屏幕

  iPhone应用开发学习笔记是本文要介绍的内容,主要讲解了iphone如何读取txt文件、利用WebView在ipad下实现滚动分页效果、iPhone抓图程序的内容,来看详细内容  。

  iphone读取txt文件

  读取一般性文档文件 

  1. NSString *tmp;   
  2. NSArray *lines;    
  3. lines = [[NSString    stringWithContentsOfFile:@"testFileReadLines.txt"]   
  4.               componentsSeparatedByString:@"\n"];   
  5.  
  6. NSEnumerator *nse = [lines objectEnumerator];   
  7.  
  8. // 读取<>里的内容   
  9. while(tmp = [nse nextObject]) {   
  10.           NSString *stringBetweenBrackets = nil;   
  11.           NSScanner *scanner = [NSScanner scannerWithString:tmp];   
  12.           [scanner scanUpToString:@"<" intoString:nil];   
  13.           [scanner scanString:@"<" intoString:nil];   
  14.           [scanner scanUpToString:@">" intoString:&stringBetweenBrackets];   
  15.  
  16.           NSLog([stringBetweenBrackets description]);   
  17.   }  

  利用WebView在ipad下实现滚动分页效果

  WebView里面的网页,滚动的时候默认是平滑滚动的,如果需要让它实现分页的滚动效果,那么如何做?

  默认UIWebView是没有API提供的,但是在sdk3.2下,它的第一个子View是UIScrollView(注意对于3.2之下的版本是UIScroller一个私有未公开的,这个暂时没研究如何设置).

  代码相对比较简单:

  1. int height = webView.frame.size.height;  
  2.  
  3. NSString *html = [NSString stringWithFormat:@"<html><head><style>div{height:%dpx;
  4. }
  5. style>head><body style=margin:0px><div style=background-color:#FF0000;>
  6. div><div style=background-color:#FFFF00;>div><div style=background-color:#FF00FF;>
  7. div><div style=background-color:#0000FF;>div><div style=background-color:#00FFFF;>
  8. div><div style=background-color:#00FF00;>div>body>html>",  
  9.   height];  
  10.  
  11. [webView loadHTMLString:html baseURL:nil];  
  12. UIScrollView *scrollView = [webView.subviews objectAtIndex:0]; // it is "UIScroller" on iphone(v3.1.3-)  
  13. if (scrollView && [scrollView isKindOfClass:[UIScrollView class]]) {  
  14.     scrollView.pagingEnabled = YES;  
  15. }  

  iPhone抓图程序

  //获得屏幕图像

  1. - (UIImage *)imageFromView: (UIView *) theView    
  2. {  
  3.       
  4.     UIGraphicsBeginImageContext(theView.frame.size);  
  5.     CGContextRef context = UIGraphicsGetCurrentContext();  
  6.     [theView.layer renderInContext:context];  
  7.     UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  
  8.     UIGraphicsEndImageContext();  
  9.       
  10.     return theImage;  

  //获得某个范围内的屏幕图像

  1. - (UIImage *)imageFromView: (UIView *) theView   atFrame:(CGRect)r  
  2. {  
  3.     UIGraphicsBeginImageContext(theView.frame.size);  
  4.     CGContextRef context = UIGraphicsGetCurrentContext();  
  5.     CGContextSaveGState(context);  
  6.     UIRectClip(r);  
  7.     [theView.layer renderInContext:context];  
  8.     UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  
  9.     UIGraphicsEndImageContext();  
  10.       
  11.     return  theImage;//[self getImageAreaFromImage:theImage atFrame:r];  

  小结:iPhone应用开发学习笔记的内容介绍完了,希望本文对你有所帮助!