生活随笔
收集整理的這篇文章主要介紹了
iPhone/iOS图片相关(读取、保存、绘制、其它相关)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一.讀取圖片
1.從資源(resource)讀取
[cpp] view plaincopyprint?
UIImage*?image=[UIImage?imageNamed:@"1.jpg"];??
2.從網(wǎng)絡(luò)讀取
[cpp] view plaincopyprint?
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.從手機(jī)本地讀取
[cpp] view plaincopyprint?
??NSString?*aPath3=[NSString?stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];??UIImage?*imgFromUrl3=[[UIImage?alloc]initWithContentsOfFile:aPath3];??UIImageView*?imageView3=[[UIImageView?alloc]initWithImage:imgFromUrl3];?? 4.從現(xiàn)有的context中獲得圖像
[cpp] view plaincopyprint?
??CGImageSourceRef?source?=?CGImageSourceCreateWithURL((CFURLRef)url,?NULL);??CGImageRef?img=?CGImageSourceCreateImageAtIndex(source,0,NULL);??CGContextRef?ctx=UIGraphicsGetCurrentContext();??CGContextSaveGState(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.轉(zhuǎn)換成NSData來保存圖片(imgFromUrl是UIImage)
[cpp] view plaincopyprint?
????????NSString?*aPath=[NSString?stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];??NSData?*imgData?=?UIImageJPEGRepresentation(imgFromUrl,0);??????[imgData?writeToFile:aPath?atomically:YES];?????
2.用Quartz的CGImageDestinationRef來輸出圖片,這個方式不常見,所以不做介紹,詳細(xì)可以看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];?? 2.[img drawAtPoint]系列方法
[cpp] view plaincopyprint?
[image4?drawAtPoint:CGPointMake(100,?0)];???? 3.CGContextDrawImage
[cpp] view plaincopyprint?
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);????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];??
四.其它
1.CGImage和UIImage互換
這樣就可以隨時切換UIKit和Quartz之間類型,并且選擇您熟悉的方式來處理圖片.
CGImage cgImage=[uiImage CGImage];
UIImage* uiImage=[UIImage imageWithCGImage:cgImage];
2.UIImage resizableImageWithCapInsets的問題
假設(shè)一張44x29的圖片,同樣的Insets=UIEdgeInsetsMake(10,10,10,10)在@2x情況和非@2x情況下,表現(xiàn)會有不同,非@2x是OK正常的,但是如果同樣尺寸的圖片變成@2x,則導(dǎo)致在切換過渡的時候會很卡,應(yīng)該是在不同的重繪導(dǎo)致的,表面原因是因為Insets設(shè)置的是點,在@2x情況下拉伸,其實拉升的像素是上面20,下面也是20,但是圖片其實只有29,所以導(dǎo)致不正確,只要將insets設(shè)置成=UIEdgeInsetsMake(5,10,5,10)就正常了,所以以后要注意了。
3.動畫圖片使用注意
animationImage 設(shè)置完畢以后要startAnimation.不會自動啟動動畫圖片。
此外在讀取大量動畫圖片的時候不太適合用這個方法,因為一下子那么多圖片容易爆掉。可以用這個方法替代,具體我也沒試,方法就是手動切換圖片,并非直接使用系統(tǒng)方法而已。
[cpp] view plaincopyprint?
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;???}??? 相關(guān)帖子:http://www.cocoachina.com/bbs/read.php?tid=110154
4.UIControl設(shè)置UIImage
問題描述主要是有一個很小的叉按鈕,需要響應(yīng)很大的點擊區(qū)域,這個其實很簡單,代碼如下:
[cpp] view plaincopyprint?
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?setImage:bg?forState:UIControlStateNormal];?? 此外多說一句,這個icon圖片如果要準(zhǔn)備2套圖,縮放畢竟消耗效率
縮放圖片代碼
[cpp] view plaincopyprint?
-(UIImage?*)scaleImage:(UIImage?*)img?ToSize:(CGSize)itemSize{????UIImage?*i;????UIGraphicsBeginImageContext(itemSize);??CGRect?imageRect=CGRectMake(0,?0,?itemSize.width,?itemSize.height);??[img?drawInRect:imageRect];??i=UIGraphicsGetImageFromCurrentImageContext();??UIGraphicsEndImageContext();??return?i;?? 從view截圖出來
[cpp] view plaincopyprint?
#import?<QuartzCore/QuartzCore.h>????-(UIImage?*)getImageFromView:(UIView?*)orgView{??????UIGraphicsBeginImageContext(orgView.bounds.size);??????[orgView.layer?renderInContext:UIGraphicsGetCurrentContext()];??????UIImage?*image?=?UIGraphicsGetImageFromCurrentImageContext();??????UIGraphicsEndImageContext();???????????return?image;??}??
http://blog.csdn.net/jerryvon/article/details/7526147
總結(jié)
以上是生活随笔為你收集整理的iPhone/iOS图片相关(读取、保存、绘制、其它相关)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。