UIImage圖片處理,旋轉、截取、平鋪、縮放等操作

顏色相關

1.根據顏色生成純色圖片
就是根據制定的顏色生成一張純色的圖片

+ (UIImage *)imageWithColor:(UIColor *)color;

使用方法,比如設置UIImageView的圖片爲紅色純圖片:

self.imageView.image = [UIImage imageWithColor:[UIColor redColor]];

2.取圖片上某一像素的顏色
有時候我們需要獲取圖片上的某一點的顏色,比如畫板應用選擇畫筆顏色的時候,其實是在一張有所有顏色的圖片上點擊選擇實現的。
需要注意的是這裏要傳的參數point是相對於圖片上的點。

- (UIColor *)colorAtPixel:(CGPoint)point;

使用方法,比如我們在圖片上加個tap手勢,然後在響應方法裏面這樣寫就可以了:

- (void)handleTap:(UITapGestureRecognizer *)tap
{    CGPoint point = [tap locationInView:tap.view];    UIImage *image = self.imageView.image;    CGPoint pointInImage = CGPointMake(point.x * image.size.width / self.imageView.frame.size.width, point.y * image.size.height / self.imageView.frame.size.height);    self.view.backgroundColor = [image colorAtPixel:pointInImage];
}

3.獲得灰度圖
獲取一張彩色圖片的黑白圖片

- (UIImage *)convertToGrayImage;

使用方法:

self.imageView.image = [image convertToGrayImage];

旋轉相關

1.糾正圖片的方向
當我們需要讀取相冊的圖片,發現相冊裏面的方向和展示出來的圖片的方向不一樣,這時候就要矯正方向了。

- (UIImage *)fixOrientation;

使用:

self.imageView.image = [image fixOrientation];

2.按給定的方向旋轉圖片
在做圖片處理工具的時候,我們可能需要旋轉圖片。
這個方法的參數是系統枚舉UIImageOrientation。

typedef NS_ENUM(NSInteger, UIImageOrientation) {    UIImageOrientationUp,            // default orientation
    UIImageOrientationDown,          // 180 deg rotation
    UIImageOrientationLeft,          // 90 deg CCW
    UIImageOrientationRight,         // 90 deg CW
    UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
    UIImageOrientationDownMirrored,  // horizontal flip
    UIImageOrientationLeftMirrored,  // vertical flip
    UIImageOrientationRightMirrored, // vertical flip};

- (UIImage*)rotate:(UIImageOrientation)orient;

使用,比如順時針旋轉180度:

self.imageView.image = [image rotate:UIImageOrientationDown];

3.垂直翻轉
其實就是上面的方法傳UIImageOrientationDownMirrored參數。

- (UIImage *)flipVertical;

4.水平翻轉
其實就是傳UIImageOrientationUpMirrored參數。

- (UIImage *)flipHorizontal;

5.將圖片旋轉degrees角度
傳入一個自定義的角度。

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;

6.將圖片旋轉radians弧度

- (UIImage *)imageRotatedByRadians:(CGFloat)radians;

生成圖相關

1.截取image對象rect區域內的圖像

- (UIImage *)subImageWithRect:(CGRect)rect;

2.壓縮圖片至指定尺寸

- (UIImage *)rescaleImageToSize:(CGSize)size;

3.壓縮圖片至指定像素

- (UIImage *)rescaleImageToPX:(CGFloat )toPX;

4.生成一個size大小的平鋪圖片

- (UIImage *)getTiledImageWithSize:(CGSize)size;

5..UIView轉化爲UIImage

  • (UIImage )imageFromView:(UIView )view;

6.將兩個圖片生成一張圖片
firstImage在下面,secondImage在上面

  • (UIImage*)mergeImage:(UIImage*)firstImage withImage:(UIImage*)secondImage;
這裏寫代碼片

Gif相關

將一個Gif直接設置爲UIImageView的image就可以顯示動態圖了。

/** 用一個Gif生成UIImage,傳入一個GIFData */+ (UIImage *)animatedImageWithAnimatedGIFData:(NSData *)theData;
/** 用一個Gif生成UIImage,傳入一個GIF路徑 */+ (UIImage *)animatedImageWithAnimatedGIFURL:(NSURL *)theURL;

使用:

NSString *path = [[NSBundle mainBundle] pathForResource:@"gif" ofType:@"gif"];
self.imageView.image = [UIImage animatedImageWithAnimatedGIFURL:[NSURL fileURLWithPath:path]];
//或者
self.imageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfFile:path]];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章