1. 程式人生 > >iOS 讀取圖片的各種方法

iOS 讀取圖片的各種方法

一.讀取圖片

1.從資源(resource)讀取 [cpp] view plaincopyprint?
  1. UIImage* image=[UIImage imageNamed:@"1.jpg"];  

2.從網路讀取 [cpp] view plaincopyprint?
  1. NSURL *url=[NSURL URLWithString:@"http://attach.bbs.miui.com/forum/201203/20/170226n5qcwdpusnjdsswy.jpg"];  
  2. UIImage *imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];  

3.從手機本地讀取 [cpp] view plaincopyprint?
  1. //讀取本地圖片非resource
  2. NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];  
  3. UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];  
  4. UIImageView* imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];  
4.從現有的context中獲得影象 [cpp] view plaincopyprint?
  1. //add ImageIO.framework and #import   
  2. CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);  
  3. CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);  
  4. CGContextRef ctx=UIGraphicsGetCurrentContext();  
  5. CGContextSaveGState(ctx);  
  6. //transformCTM的2種方式
  7. //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(.2, -0.2));
  8. //CGContextScaleCTM(ctx,1,-1);
  9. //注意座標要反下,用ctx來作為圖片源 
  10. CGImageRef capture=CGBitmapContextCreateImage(ctx);  
  11. CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);  
  12. CGContextDrawImage(ctx, CGRectMake(160, 230, 160, 230), img);  
  13. CGImageRef capture2=CGBitmapContextCreateImage(ctx);  

5.用Quartz的CGImageSourceRef來讀取圖片 [cpp] view plaincopyprint?
  1. CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);  
  2. CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);  

二.儲存圖片

1.轉換成NSData來儲存圖片(imgFromUrl是UIImage) [cpp] view plaincopyprint?
  1. //儲存圖片 2種獲取路徑都可以
  2. //NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  3. //NSString*documentsDirectory=[paths objectAtIndex:0];  
  4. //NSString*aPath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",@"test"]]; 
  5. NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];  
  6. NSData *imgData = UIImageJPEGRepresentation(imgFromUrl,0);      
  7. [imgData writeToFile:aPath atomically:YES];     

2.用Quartz的CGImageDestinationRef來輸出圖片,這個方式不常見,所以不做介紹,詳細可以看apple文件Quartz 2D Programming Guide

三.繪製圖(draw|painting)

1.UIImageView方式加入到UIView層 [cpp] view plaincopyprint?
  1. UIImageView* imageView=[[UIImageView alloc]initWithImage:image];  
  2. imageView.frame=CGRectMake(0, 0, 320, 480);  
  3. [self addSubview:imageView];  
  4. [imageView release];  
2.[img drawAtPoint]系列方法 [cpp] view plaincopyprint?
  1. [image4 drawAtPoint:CGPointMake(100, 0)];    
3.CGContextDrawImage [cpp] view plaincopyprint?
  1. CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);  

4.CGLayer 這個是apple推薦的一種offscreen的繪製方法,相比bitmapContext更好,因為它似乎會利用iphone硬體(drawing-card)加速 [cpp] view plaincopyprint?
  1. CGLayerRef cg=CGLayerCreateWithContext(ctx, CGSizeMake(320, 480), NULL);  
  2. //需要將CGLayerContext來作為快取context,這個是必須的
  3. CGContextRef layerContext=CGLayerGetContext(cg);  
  4. CGContextDrawImage(layerContext, CGRectMake(160, 230, 160, 230), img);   
  5. CGContextDrawLayerAtPoint(ctx, CGPointMake(0, 0), cg);  

5.CALayer的contents [cpp] view plaincopyprint?
  1. UIImage* image=[UIImage imageNamed:@"1.jpg"];  
  2. CALayer *ly=[CALayer layer];  
  3. ly.frame=CGRectMake(0, 0, 320, 460);  
  4. ly.contents=[image CGImage];  
  5. [self.layer addSublayer:ly];  


四.其它

1.CGImage和UIImage互換 這樣就可以隨時切換UIKit和Quartz之間型別,並且選擇您熟悉的方式來處理圖片. CGImage cgImage=[uiImage CGImage]; UIImage* uiImage=[UIImage imageWithCGImage:cgImage];