溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS中如何實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)

發(fā)布時(shí)間:2021-08-04 13:47:43 來(lái)源:億速云 閱讀:184 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要介紹iOS中如何實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

如果需要實(shí)現(xiàn)對(duì)imageView實(shí)現(xiàn)一定角度的旋轉(zhuǎn),具體步驟是:

      1.將image轉(zhuǎn)成context。

      2.對(duì)context進(jìn)行一定角度的旋轉(zhuǎn)。

      3.將旋轉(zhuǎn)后的context 轉(zhuǎn)化成image。

經(jīng)過(guò)這三個(gè)步驟,我們就能夠?qū)崿F(xiàn)將圖片真正的做到旋轉(zhuǎn)。

 好了,直接上代碼:

#import"UIImage+RotateImageTool.h"
#import<QuartzCore/QuartzCore.h>
#import<Accelerate/Accelerate.h>
@implementationUIImage (RotateImageTool)
-(UIImage*)rotateImageWithDegree:(CGFloat)degree{
//將image轉(zhuǎn)化成context
//獲取圖片像素的寬和高
size_t width =self.size.width*self.scale;
size_t height =self.size.height*self.scale;
//顏色通道為8因?yàn)?-255經(jīng)過(guò)了8個(gè)顏色通道的變化
//每一行圖片的字節(jié)數(shù)因?yàn)槲覀儾捎玫氖茿RGB/RGBA所以字節(jié)數(shù)為width * 4
size_t bytesPerRow =width *4;
//圖片的透明度通道
CGImageAlphaInfo info =kCGImageAlphaPremultipliedFirst;
//配置context的參數(shù):
CGContextRef context =CGBitmapContextCreate(nil, width, height,8, bytesPerRow,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault|info);
if(!context) {
return nil;
}
//將圖片渲染到圖形上下文中
CGContextDrawImage(context,CGRectMake(0,0, width, height),self.CGImage);
uint8_t* data = (uint8_t*)CGBitmapContextGetData(context);
//旋轉(zhuǎn)欠的數(shù)據(jù)
vImage_Buffer src = { data,height,width,bytesPerRow};
//旋轉(zhuǎn)后的數(shù)據(jù)
vImage_Buffer dest= { data,height,width,bytesPerRow};
//背景顏色
Pixel_8888 backColor = {0,0,0,0};
//填充顏色
vImage_Flags flags = kvImageBackgroundColorFill;
//旋轉(zhuǎn)context 
vImageRotate_ARGB8888(&src, &dest,nil, degree *M_PI/180.f, backColor, flags);
//將conetxt轉(zhuǎn)換成image
CGImageRef imageRef =CGBitmapContextCreateImage(context);
UIImage* rotateImage =[UIImageimageWithCGImage:imageRefscale:self.scaleorientation:self.imageOrientation];
returnrotateImage;
}

iOS中如何實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)

代碼中有詳細(xì)的注釋,在這里我就不過(guò)多的解釋了。感興趣的可以到github上面下載哦。

下載地址:github.com/15221532825/ImageTool  (本地下載)

附:iOS ImageView的Image自適應(yīng)縮放顯示全套處理方法

// retina屏幕圖片顯示問(wèn)題
[_detailImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
// 不規(guī)則圖片顯示
_detailImageView.contentMode = UIViewContentModeScaleAspectFill;
_detailImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// 圖片大于或小于顯示區(qū)域
_detailImageView.clipsToBounds = YES;

以上是“iOS中如何實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI