iOS中實現imageView任意角度旋轉的方法

2022-09-21 22:54:08 字數 1774 閱讀 6627

前言

在實際的開發中我們可能會遇程式設計客棧到這種情況: 需要對進行一定角度的旋轉。對於這種需要,我們可能會用uiview的transform進行旋轉,但是這樣做其實只是對承載imageview的view進行了一定角度的旋轉,而imageview並沒有旋轉。所有這樣的做法並不好。

如果需要實現對imageview實現一定角度的旋轉,具體步驟是:

1.將image轉成context。

2.對context進行一定角度的旋轉。

www.cppcns.com  3.將旋轉後的context 轉化成image。

經過這三個步驟,我們就能夠實現將真正的做到旋轉。

好了,直接上**:

#import"uiimage+rotateimagetool.h"

#import

#import

@implementationuiimage (rotateimagetool)

-(uiimage*)rotateimagewithdegree:(cgfloat)degree

//將渲染到圖形上下文中

cgcontextdrawimage(context,cgrectmake(0,0, width, height),self.cgimage);

uint8_t* data = (uint8_t*)cgbitmapcontextgetdata(context);

//旋轉欠的資料

vimage_buffer src = ;

//旋轉後的資料

vimage_buffer dest= ;

//背景顏色

pixel_8888 backcolor = ;

//填充顏色

vimage_flags flags = kvimagebackgroundcolorfill;

//旋轉context

vimagerotate_argb8888(&src, &dest,nil, degree *m_pi/180.f, backcolor, flags);

//將conetxt轉換成image

cgimageref imageref =cgbitmapcontextcreateimage(context);

uiimage* rotateimage =[uiimageimagewithcgimage:imagerefscale:self.scaleorientation:self.imageorientation];

returnrotateimage;

}**中有詳細的注釋,在這裡我就不過多的解釋了。感興趣的可以到github上面**哦。

**位址:github.com/15221532825/imagetool&mxlirpnbsp; (本地**)

附:ios imageview的image自適應縮放顯示全套處理方法

// retina螢幕顯示問題

[_detailimageview setcontentscale程式設計客棧factor:[[uiscreen mainscreen] scale]];

// 不規則顯示

_detailimageview.contentmode = uiviewcontentmodescaleaspectfill;

_detailimageview.autoresizingmask = uiviewautoresizingflexibleheight;

// 大於或小於顯示區域

_detailimageview.clipstobounds = yes;

總結本文標題: ios中實現imageview任意角度旋轉的方法

本文位址:

iOS 在cell中修改imageView的大小

今天發現在cell中修改imageview的大小時,用frame.size 或者是frame之類的都報錯,然後找到一種解決辦法,直接上 cgsize itemsize cgsizemake 40,40 uigraphicsbeginimagecontextwithoptions itemsize,n...

Android實現圓角ImageView

android實現圓角imageview code 效果圖如下 實現步驟 1 新增roundangleimageview檔案,實現自定義view。原理 1 重新繪製,採用mode.dst out去掉重疊部分的原理,先製作重貼部分,刪除重疊部分即可2 分別用原理1,在4個角製作出圓角部分,去掉4個角 ...

android 實現圓角imageview

clip無法實現抗鋸齒,建議把clippath改為使用xfermode!實現原理,通過裁切canvas來控制繪製效果,canvas有drawroundrect可以直接繪製圓角矩形,但是沒有直接裁切圓角矩形的方法。如果想這麼做,就需要通過path來裁切canvas了 讓自己的類繼承自imageview...