iPhone/iOS圖片相關(讀取、儲存、繪製、其它相關)

來源:互聯網
上載者:User
一.讀取圖片1.從資源(resource)讀取
UIImage* image=[UIImage imageNamed:@"1.jpg"];

2.從網路讀取

NSURL *url=[NSURL URLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"];UIImage *imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];

3.從手機本地讀取

//讀取本地圖片非resourceNSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];UIImageView* imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];

4.從現有的context中獲得映像

//add ImageIO.framework and #import <ImageIO/ImageIO.h>  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來讀取圖片

CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
二.儲存圖片1.轉換成NSData來儲存圖片(imgFromUrl是UIImage)
//儲存圖片 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層

UIImageView* imageView=[[UIImageView alloc]initWithImage:image];imageView.frame=CGRectMake(0, 0, 320, 480);[self addSubview:imageView];[imageView release];

2.[img drawAtPoint]系列方法

[image4 drawAtPoint:CGPointMake(100, 0)];  

3.CGContextDrawImage

CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);

4.CGLayer這個是apple推薦的一種offscreen的繪製方法,相比bitmapContext更好,因為它似乎會利用iphone硬體(drawing-card)加速

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

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];
四.其它1.CGImage和UIImage互換這樣就可以隨時切換UIKit和Quartz之間類型,並且選擇您熟悉的方式來處理圖片.CGImage cgImage=[uiImage CGImage];UIImage* uiImage=[UIImage imageWithCGImage:cgImage];2.UIImage resizableImageWithCapInsets的問題假設一張44x29的圖片,同樣的Insets=UIEdgeInsetsMake(10,10,10,10)在@2x情況和非@2x情況下,表現會有不同,非@2x是OK正常的,但是如果同樣尺寸的圖片變成@2x,則導致在切換過渡的時候會很卡,應該是在不同的重繪導致的,表面原因是因為Insets設定的是點,在@2x情況下展開,其實拉升的像素是上面20,下面也是20,但是圖片其實只有29,所以導致不正確,只要將insets設定成=UIEdgeInsetsMake(5,10,5,10)就正常了,所以以後要注意了。3.動畫圖片使用注意

animationImage 設定完畢以後要startAnimation.不會自動啟動動畫圖片。

此外在讀取大量動畫圖片的時候不太適合用這個方法,因為一下子那麼多圖片容易爆掉。可以用這個方法替代,具體我也沒試,方法就是手動切換圖片,並非直接使用系統方法而已。

imgV=[[UIImageView alloc]initWithFrame:CGRectMake(40, 40, 128, 128)];[self.window addSubview:imgV];[self performSelectorInBackground:@selector(playAnim)withObject:nil];[imgV release];-(void)playAnim{for (int i=0;i<101;){usleep(100000);UIImage *image=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i+1 ] ofType:@"tiff"]];[self performSelectorOnMainThread:@selector(changeImage:) withObject:image waitUntilDone:YES];i++;}} -(void)changeImage:(UIImage*)image{imgV.image=image; } 

相關文章:http://www.cocoachina.com/bbs/read.php?tid=110154

4.UIControl設定UIImage

問題描述主要是有一個很小的叉按鈕,需要響應很大的點擊地區,這個其實很簡單,代碼如下:

   UIImage *bg=[UIImage imageNamed:@"heizi1.jpg"];        //圖片大於點及地區,縮小下就行        bg=[self scaleImage:bg ToSize:(CGSize){100,100}];        UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];        //圖片大於button,則會被展開,如果小於button則置中顯示        [button setImage:bg forState:UIControlStateNormal];

此外多說一句,這個icon圖片如果要準備2套圖,縮放畢竟消耗效率

縮放圖片代碼

        -(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize{                UIImage *i;        //    CGSize itemSize=CGSizeMake(30, 30);        UIGraphicsBeginImageContext(itemSize);        CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);        [img drawInRect:imageRect];        i=UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return i;}

從view出來

#import <QuartzCore/QuartzCore.h>-(UIImage *)getImageFromView:(UIView *)orgView{    UIGraphicsBeginImageContext(orgView.bounds.size);    [orgView.layer renderInContext:UIGraphicsGetCurrentContext()];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();       return image;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.