首页 技术 正文
技术 2022年11月7日
0 收藏 873 点赞 967 浏览 1890 个字

一.目的:

有时候我们获得到的图片我们不是我们想要的方向,需要对图片进行旋转.比如:图片旋转90度180度等.

二.实现过程.

1.获取到该UIImage.

2.开启上下文.

3.上下文的具体操作.

4. 将上下文转化为想要的UIImage

三.UIImage 的 Category

UIImage+ImageRotate.h

#import <UIKit/UIKit.h>@interface UIImage (ImageRotate)- (UIImage *)imageRotateWithIndegree:(CGFloat)indegree;@end

UIImage+ImageRotate.m

#import "UIImage+ImageRotate.h"
#import <QuartzCore/QuartzCore.h>
#import <Accelerate/Accelerate.h>@implementation UIImage (ImageRotate)#pragma mark - 图片旋转// 1 image --> Context 2. context 3. context --> image- (UIImage *)imageRotateWithIndegree:(CGFloat)indegree { // 1. image --> context
size_t width = (size_t)(self.size.width * self.scale);
size_t height = (size_t)(self.size.height * self.scale); size_t bytesPerRow = width * ; // 每行图片字节数
CGImageAlphaInfo alphaInfo = kCGImageAlphaPremultipliedFirst; // alpha // 配置上下文
// CGColorRef bmContext = CGBitmapContextCreate(<#void * _Nullable data#>, <#size_t width#>, <#size_t height#>, <#size_t bitsPerComponent#>, <#size_t bytesPerRow#>, <#CGColorSpaceRef _Nullable space#>, <#uint32_t bitmapInfo#>)
CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, , bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault | alphaInfo); if (!bmContext) {
return nil;
} CGContextDrawImage(bmContext, CGRectMake(, , width, height), self.CGImage); // 2. 旋转
UInt8 * data = (UInt8 *)CGBitmapContextGetData(bmContext); // 需要引入 #import <Accelerate/Accelerate.h> 解释这个类干什么用的
vImage_Buffer src = {data,height,width,bytesPerRow};
vImage_Buffer dest = {data,height,width,bytesPerRow};
Pixel_8888 bgColor = {,,,};
vImageRotate_ARGB8888(&src, &dest, NULL, indegree, bgColor, kvImageBackgroundColorFill); // 3. context --> UIImage
CGImageRef rotateImageRef = CGBitmapContextCreateImage(bmContext);
UIImage * rotateImage = [UIImage imageWithCGImage:rotateImageRef scale:self.scale orientation:self.imageOrientation]; CGContextRelease(bmContext);
CGImageRelease(rotateImageRef); return rotateImage;
}@end

四.使用

 UIImage * image = [image imageRotateWithIndegree:M_PI / 2];

github地址: https://github.com/mancongiOS/UIImage.git

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849