IOS UIImage類方法總結

來源:互聯網
上載者:User

標籤:blog   os   io   使用   ar   for   檔案   資料   art   

IOS中對圖片的處理  UIImage相信做項目時肯定會有用到 UIImage 這個類,那我們就來看一下這個類中都有什麼內容。其實這篇文章就是在看文檔的時候想記錄一下文檔中得方法。UIImage  繼承於NSObject下面介紹一下UIImage中的方法首先是我們最常用的通過圖片的檔案名稱來擷取這個圖片+ (UIImage *)imageNamed:(NSString *)name//要注意的是這個方法適用於已經匯入到工程中的圖片建立新圖片1、+ (UIImage *)imageWithContentsOfFile:(NSString *)path//通過檔案載入指定路徑下的檔案內容獲得新圖片2、+ (UIImage *)imageWithData:(NSData *)data//通過一個NSData對象來獲得圖片3、+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale//同上,只是再加上一個圖片大小比例,用來改變圖片的大小4、+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage//使用Quartz 2D對象建立UIImage5、+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation//制定圖片的比例和方向,其中方向是個枚舉類。6、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage//用一個Core Image 對象建立映像7、+ (UIImage *)imageWithCIImage:(CIImage *)ciImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation//再加上比例和圖片方向8、- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets//返回指定矩形地區內的映像9、+ (UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)duration//建立一個動態圖片,動態圖片持續的時間為duration10、+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration//用一組圖片建立一個動態圖片,動態期間duration11、+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets duration:(NSTimeInterval)duration//建立一個在可變大小的圖片上指定矩形地區內的動態圖片12、+ (UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode duration:(NSTimeInterval)duration//同上,只是多了一個圖片變化的方式,具體來說就是平鋪或者展開13、- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets//用制定矩形地區建立映像14、- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode//同上,指定圖片變化方式初始化圖片  方法的作用在從上面的一些方法中都能找到原型,這裡就不一一注釋了1、– initWithContentsOfFile:  //從檔案載入圖片2、– initWithData:                  //用NSData對象初始化圖片3、– initWithData:scale:      //用NSData對象,指定的比例,初始化圖片4、– initWithCGImage:5、– initWithCGImage:scale:orientation:6、– initWithCIImage:7、– initWithCIImage:scale:orientation:繪畫圖片1、– drawAtPoint://在指定的點開始繪畫圖片,這個點就是圖片的做上角頂點2、- (void)drawAtPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha//在指定的點繪製整個圖片,並使用自訂圖片複合模式,並設定透明度3、– drawInRect://在指定地區內繪製圖片,可根據需要縮放圖片4、– drawInRect:blendMode:alpha://參照上面第二條5、– drawAsPatternInRect://在指定地區內,平鋪圖片image的屬性  imageOrientation   //圖片的方向   size   //圖片的大小size   scale   //圖片的比例   resizingMode  //圖片變化方式   CGImage  //潛在的Quartz image   CIImage  //潛在的Core Image   images  //返回一個由圖片組成的數組,針對於由一組圖片產生的動態圖片   duration  //返回動態圖片持續的時間(即動態圖片播放一遍的時間)   capInsets  //圖片上選定的地區   alignmentRectInsets  //圖片平鋪的地區iOS內建的提供了一個API如下[html] view plaincopy NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);    在Iphone上有兩種讀取圖片資料的簡單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函數需要兩個參數:圖片的引用和壓縮係數.而UIImagePNGRepresentation只需要圖片引用作為參數.通過在實際使用過程中,比較發現: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的圖片資料量大很多.譬如,同樣是讀取網路攝影機拍攝的同樣景色的照片, UIImagePNGRepresentation()返回的資料量大小為199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的資料量大小隻為140KB,比前者少了50多KB.如果對圖片的清晰度要求不高,還可以通過設定 UIImageJPEGRepresentation函數的第二個參數,大幅度降低圖片資料量.譬如,剛才拍攝的圖片, 通過調用UIImageJPEGRepresentation(UIImage* image, 1.0)讀取資料時,返回的資料大小為140KB,但更改壓縮係數後,通過調用UIImageJPEGRepresentation(UIImage* image, 0.5)讀取資料時,返回的資料大小隻有11KB多,大大壓縮了圖片的資料量 ,而且從視角角度看,圖片的品質並沒有明顯的降低.因此,在讀取圖片資料內容時,建議優先使用UIImageJPEGRepresentation,並可根據自己的實際使用情境,設定壓縮係數,進一步降低圖片資料量大小。[html] view plaincopy UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];  imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)];  NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001);    m_selectImage = [UIImage imageWithData:imageData];  .h具體code [html] view plaincopy #import <Foundation/Foundation.h>    @interface UIImage (UIImageExt)    - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;    - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;  @end  .m具體code[html] view plaincopy #import "UIImageExt.h"      @implementation UIImage (UIImageExt)    - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{      // 建立一個bitmap的context      // 並把它設定成為當前正在使用的context      UIGraphicsBeginImageContext(size);      // 繪製改變大小的圖片      [img drawInRect:CGRectMake(0, 0, size.width, size.height)];      // 從當前context中建立一個改變大小後的圖片      UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();      // 使當前的context出堆棧      UIGraphicsEndImageContext();      // 返回新的改變大小後的圖片      return scaledImage;  }        - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize  {      UIImage *sourceImage = self;      UIImage *newImage = nil;      CGSize imageSize = sourceImage.size;      CGFloat width = imageSize.width;      CGFloat height = imageSize.height;      CGFloat targetWidth = targetSize.width;      CGFloat targetHeight = targetSize.height;      CGFloat scaleFactor = 0.0;      CGFloat scaledWidth = targetWidth;      CGFloat scaledHeight = targetHeight;      CGPoint thumbnailPoint = CGPointMake(0.0,0.0);            if (CGSizeEqualToSize(imageSize, targetSize) == NO)      {          CGFloat widthFactor = targetWidth / width;          CGFloat heightFactor = targetHeight / height;                    if (widthFactor > heightFactor)              scaleFactor = widthFactor; // scale to fit height          else              scaleFactor = heightFactor; // scale to fit width          scaledWidth  = width * scaleFactor;          scaledHeight = height * scaleFactor;                    // center the image          if (widthFactor > heightFactor)          {              thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;          }          else              if (widthFactor < heightFactor)              {                  thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;              }      }            UIGraphicsBeginImageContext(targetSize); // this will crop            CGRect thumbnailRect = CGRectZero;      thumbnailRect.origin = thumbnailPoint;      thumbnailRect.size.width  = scaledWidth;      thumbnailRect.size.height = scaledHeight;            [sourceImage drawInRect:thumbnailRect];            newImage = UIGraphicsGetImageFromCurrentImageContext();      if(newImage == nil)          NSLog(@"could not scale image");            //pop the context to get back to the default      UIGraphicsEndImageContext();      return newImage;  }    @end  

  

IOS UIImage類方法總結

聯繫我們

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