新聞中心
這篇文章主要介紹iOS中如何實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),達(dá)川網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:達(dá)川等地區(qū)。達(dá)川做網(wǎng)站價(jià)格咨詢:18980820575
如果需要實(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)過這三個(gè)步驟,我們就能夠?qū)崿F(xiàn)將圖片真正的做到旋轉(zhuǎn)。
好了,直接上代碼:
#import"UIImage+RotateImageTool.h" #import#import @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)過了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; }
代碼中有詳細(xì)的注釋,在這里我就不過多的解釋了。感興趣的可以到github上面下載哦。
下載地址:github.com/15221532825/ImageTool (本地下載)
附:iOS ImageView的Image自適應(yīng)縮放顯示全套處理方法
// retina屏幕圖片顯示問題 [_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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁名稱:iOS中如何實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)
網(wǎng)址分享:http://www.ef60e0e.cn/article/gjhchh.html