iOS 讀取圖片的各種方法
阿新 • • 發佈:2019-01-02
一.讀取圖片
1.從資源(resource)讀取 [cpp] view plaincopyprint?- UIImage* image=[UIImage imageNamed:@"1.jpg"];
2.從網路讀取 [cpp] view plaincopyprint?
- NSURL *url=[NSURL URLWithString:@"http://attach.bbs.miui.com/forum/201203/20/170226n5qcwdpusnjdsswy.jpg"];
-
UIImage *imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];
3.從手機本地讀取 [cpp] view plaincopyprint?
- //讀取本地圖片非resource
- NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
- UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];
-
UIImageView* imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];
- //add ImageIO.framework and #import
- CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
- CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
- CGContextRef ctx=UIGraphicsGetCurrentContext();
-
CGContextSaveGState(ctx);
- //transformCTM的2種方式
- //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(.2, -0.2));
- //CGContextScaleCTM(ctx,1,-1);
- //注意座標要反下,用ctx來作為圖片源
- CGImageRef capture=CGBitmapContextCreateImage(ctx);
- CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
- CGContextDrawImage(ctx, CGRectMake(160, 230, 160, 230), img);
- CGImageRef capture2=CGBitmapContextCreateImage(ctx);
5.用Quartz的CGImageSourceRef來讀取圖片 [cpp] view plaincopyprint?
- CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
- CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
二.儲存圖片
1.轉換成NSData來儲存圖片(imgFromUrl是UIImage) [cpp] view plaincopyprint?- //儲存圖片 2種獲取路徑都可以
- //NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- //NSString*documentsDirectory=[paths objectAtIndex:0];
- //NSString*aPath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",@"test"]];
- NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
- NSData *imgData = UIImageJPEGRepresentation(imgFromUrl,0);
- [imgData writeToFile:aPath atomically:YES];
2.用Quartz的CGImageDestinationRef來輸出圖片,這個方式不常見,所以不做介紹,詳細可以看apple文件Quartz 2D Programming Guide
三.繪製圖(draw|painting)
1.UIImageView方式加入到UIView層 [cpp] view plaincopyprint?- UIImageView* imageView=[[UIImageView alloc]initWithImage:image];
- imageView.frame=CGRectMake(0, 0, 320, 480);
- [self addSubview:imageView];
- [imageView release];
- [image4 drawAtPoint:CGPointMake(100, 0)];
- CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
4.CGLayer 這個是apple推薦的一種offscreen的繪製方法,相比bitmapContext更好,因為它似乎會利用iphone硬體(drawing-card)加速 [cpp] view plaincopyprint?
- CGLayerRef cg=CGLayerCreateWithContext(ctx, CGSizeMake(320, 480), NULL);
- //需要將CGLayerContext來作為快取context,這個是必須的
- CGContextRef layerContext=CGLayerGetContext(cg);
- CGContextDrawImage(layerContext, CGRectMake(160, 230, 160, 230), img);
- CGContextDrawLayerAtPoint(ctx, CGPointMake(0, 0), cg);
5.CALayer的contents [cpp] view plaincopyprint?
- UIImage* image=[UIImage imageNamed:@"1.jpg"];
- CALayer *ly=[CALayer layer];
- ly.frame=CGRectMake(0, 0, 320, 460);
- ly.contents=[image CGImage];
- [self.layer addSublayer:ly];