任意形狀的ImageView

2021-09-24 07:12:46 字數 3909 閱讀 5744

demo位址 執行效果:

zwimageview.h中**:

#import 

@inte***ce zwimageview : uiview

/** * path:形狀

*/@property (nonatomic, assign) cgpathref path;

/** * image:

*/@property (nonatomic, strong) uiimage *image;

- (instancetype)init;

- (instancetype)initwithframe:(cgrect)frame;

- (instancetype)initwithframe:(cgrect)frame path:(cgpathref)path image:(uiimage *)image;

@end

複製**

zwimageview.m中**:

#import "zwimageview.h"

@implementation zwimageview

/*// only override drawrect: if you perform custom drawing.

// an empty implementation adversely affects performance during animation.

- (void)drawrect:(cgrect)rect

*/- (instancetype)init

return self;

}- (instancetype)initwithframe:(cgrect)frame

return self;

}- (instancetype)initwithframe:(cgrect)frame path:(cgpathref)path image:(uiimage *)image

return self;

}- (void)setuplayers

- (void)setframe:(cgrect)frame

- (void)setpath:(cgpathref)path

- (void)setimage:(uiimage *)image

@end

複製**

使用方法:

//三角形

uibezierpath *path1 = [[uibezierpath alloc] init];

path1.linecapstyle = kcglinecapround;

path1.linejoinstyle = kcglinejoinround;

[path1 movetopoint:cgpointmake(25, 0)];

[path1 addlinetopoint:cgpointmake(0, 50)];

[path1 addlinetopoint:cgpointmake(50, 50)];

[path1 fill];

zwimageview *imageview1 = [[zwimageview alloc] initwithframe:cgrectmake(20, 100, 50, 50) path:path1.cgpath image:[uiimage imagenamed:@"示例圖"]];

[self.view addsubview:imageview1];

//波浪

uibezierpath *path2 = [[uibezierpath alloc] init];

path2.linecapstyle = kcglinecapround;

path2.linejoinstyle = kcglinejoinround;

[path2 movetopoint:cgpointmake(0, 50)];

[path2 addlinetopoint:cgpointmake(0, 25)];

// [path addarcwithcenter:cgpointmake(50, 50) radius:50 startangle:m_pi endangle:m_pi_2 clockwise:yes];

[path2 addcurvetopoint:cgpointmake(50, 25) controlpoint1:cgpointmake(15, 15) controlpoint2:cgpointmake(35, 35)];

[path2 addlinetopoint:cgpointmake(50, 50)];

[path2 fill];

zwimageview *imageview2 = [[zwimageview alloc] initwithframe:cgrectmake(120, 100, 50, 50) path:path2.cgpath image:[uiimage imagenamed:@"示例圖"]];

[self.view addsubview:imageview2];

//橢圓形

uibezierpath *path3 = [uibezierpath bezierpathwithovalinrect:cgrectmake(0, 5, 50, 100)];

[path3 fill];

zwimageview *imageview3 = [[zwimageview alloc] initwithframe:cgrectmake(220, 100, 50, 120) path:path3.cgpath image:[uiimage imagenamed:@"示例圖"]];

[self.view addsubview:imageview3];

//扇形

uibezierpath *path4 = [[uibezierpath alloc] init];

[path4 movetopoint:cgpointmake(50, 100)];

[path4 addlinetopoint:cgpointmake(0, 50)];

[path4 addarcwithcenter:cgpointmake(50, 100) radius:70.7 startangle:m_pi_4 * 5 endangle:m_pi_4 * 7 clockwise:yes];

[path4 fill];

zwimageview *imageview4 = [[zwimageview alloc] initwithframe:cgrectmake(20, 180, 100, 100) path:path4.cgpath image:[uiimage imagenamed:@"示例圖"]];

[self.view addsubview:imageview4];

//四分之三圓

uibezierpath *path5 = [[uibezierpath alloc] init];

[path5 movetopoint:cgpointmake(0, 100)];

[path5 addlinetopoint:cgpointmake(0, 50)];

[path5 addarcwithcenter:cgpointmake(50, 50) radius:50 startangle:m_pi endangle:m_pi_2 clockwise:yes];

[path5 fill];

zwimageview *imageview5 = [[zwimageview alloc] initwithframe:cgrectmake(20, 300, 100, 100) path:path5.cgpath image:[uiimage imagenamed:@"示例圖"]];

[self.view addsubview:imageview5];

複製**

zwimageviewpath屬性為cgpathref型別,可以傳入任意想要的形狀。

OpenCV 擷取任意形狀的ROI

規則矩形的roi提取可以使用 numpy 的資料切片操作,但是真實場景的roi形狀並不規則,可能伴隨著旋轉角度 多點折線等特徵。這裡介紹利用 opencv 擷取任意形狀roi的基本思路。會使用到的幾個比較重要的方法有 直線擬合 cv.fitline 旋轉矩陣 cv.getrotationmatrix...

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

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

C 任意形狀按鈕控制項 實現簡單實用

上圖的6變形按鈕,是用下面的 是通過多邊形繪製出來的按鈕形狀,可以修改 point 引數實現任意形狀的按鈕。private void form1 load object sender,eventargs e int offsetx button1.size.width int offsety but...