日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iPhone/iOS图片相关(读取、保存、绘制、其它相关)

發(fā)布時間:2023/12/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?
  • //讀取本地圖片非resource??
  • 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?
  • //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);??
  • //注意坐標(biāo)要反下,用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?
  • //保存圖片?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來輸出圖片,這個方式不常見,所以不做介紹,詳細(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);??
  • //需要將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];??


  • 四.其它

    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"];??
  • ?????//圖片大于點及區(qū)域,縮小下就行??
  • ?????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圖片如果要準(zhǔn)備2套圖,縮放畢竟消耗效率

    縮放圖片代碼

    [cpp] view plaincopyprint?
  • -(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截圖出來 [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)容還不錯,歡迎將生活随笔推薦給好友。