#import <UIKit/UIKit.h>

@interface XMGImageView : UIView

/** <#注释#> */
@property (nonatomic, strong) UIImage *image; - (instancetype)initWithImage:(UIImage *)image; @end
#import "XMGImageView.h"

@implementation XMGImageView

- (instancetype)initWithImage:(UIImage *)image {

    if (self = [super init]) {
//确定当前ImageView的尺寸大小
self.frame = CGRectMake(, , image.size.width, image.size.height);
_image = image;
}
return self;
} -(void)setImage:(UIImage *)image {
_image = image;
//重绘
[self setNeedsDisplay];
} - (void)drawRect:(CGRect)rect {
// Drawing code
[self.image drawInRect:rect]; } @end
#import "ViewController.h"
#import "XMGImageView.h" @interface ViewController () /** <#注释#> */
@property (nonatomic, weak) UIImageView *imageV;
@property (nonatomic, weak) XMGImageView *xmgImageV; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // UIImageView *imageV = [[UIImageView alloc] init];
// imageV.frame = CGRectMake(0, 0, 200, 200);
// imageV.image = [UIImage imageNamed:@"CTO"];
// self.imageV = imageV;
// [self.view addSubview:imageV]; //initWithImage创建的ImageView的大小跟原始图片一样大
// UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CTO"]];
// [self.view addSubview:imageV]; XMGImageView *xmgImageV = [[XMGImageView alloc] initWithImage:[UIImage imageNamed:@"CTO"]];
[self.view addSubview:xmgImageV]; //
// XMGImageView *xmgImageV = [[XMGImageView alloc] init];
// xmgImageV.frame = CGRectMake(0, 0, 200, 200);
// xmgImageV.image = [UIImage imageNamed:@"CTO"];
// self.xmgImageV = xmgImageV;
// [self.view addSubview:xmgImageV]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //self.imageV.image = [UIImage imageNamed:@"汽水"];
self.xmgImageV.image = [UIImage imageNamed:@"汽水"]; } @end

整体思路:

我们想要模仿系统的UIImageView,我们必须得要知道系统的UIView怎么用.

系统的用法是创建一个UIImageView对象,设置frame,给它传递一个UIImage,再把它添加到一个View上面就可以了.

可以切换图片.

这是第一个用法.

第二种用法,就是在创建的时候直接传递一个UIImage对象,使用initWithImage的方法进行创建一个UImageView的方式

用这种做法创建出来的UIImageView它的尺寸大小和原始图片的尺寸大小一样大.

所以我们自己的UIImageView也要具有这些功能.

实现步骤:

第一步:新建一个UIView,起名XMGImageView.

第二步:给XMGImageView添加一个UIImage属性,供外界传递图片

第三步:在DrawRect方法当中把传递的图片绘制到View上面

绘制方法为:[_image drawInRect:rect],绘制的图片尺寸大小和UIView的尺寸大小一样大.

第四步:重写UIImage属性的set方法,在set方法当中让View重新绘制.目的为了能够办到切换图片.

第五步:提供一个- (instancetype)initWithImage:(UIImage *)image方法.

在这个方法当中重写init方法

在初始化时,让View尺寸和图片的实际大小一样大.

然后再给UIImage属性赋值.

这样在绘制图片的时候,显示出来的View已经有尺寸了, 尺寸大小和图片的实际大小一样大.

具体代码实现:

- (instancetype)initWithImage:(UIImage *)image{

if (self = [super init]) {

self.frame = CGRectMake(0, 0, image.size.width, image.size.height);

_image = image;

}

return self;

}

-(void)setImage:(UIImage *)image{

_image = image;

[self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect {

[_image drawInRect:rect];

}

iOS开发之Quartz2D 六 绘制UIImageView的更多相关文章

  1. iOS开发之Quartz2D 二:绘制直线,曲线,圆弧,矩形,椭圆,圆

    #import "DrawView.h" @implementation DrawView /** * 作用:专门用来绘图 * 什么时候调用:当View显示的时候调用 * @par ...

  2. iOS开发之Quartz2D详解

    1. 什么是Quartz2D? Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片( ...

  3. iOS开发之Quartz2D

    1.         Quartz2D概述及作用 Quartz2D的API是纯C语言的,Quartz2D的API来自于Core Graphics框架. 数据类型和函数基本都以CG作为前缀,比如: CG ...

  4. iOS开发之Quartz2D 五:UIKIT 绘图演练,画文字,画图片

    #import "DrawView.h" @implementation DrawView -(void)awakeFromNib { // //画图片 // UIImage *i ...

  5. ios开发之Quartz2D 四:画饼图

    #import "PieView.h" @implementation PieView - (void)drawRect:(CGRect)rect { // Drawing cod ...

  6. iOS开发之UISearchBar初探

    iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...

  7. iOS开发之Xcode常用调试技巧总结

    转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...

  8. 李洪强iOS开发之RunLoop的原理和核心机制

    李洪强iOS开发之RunLoop的原理和核心机制 搞iOS之后一直没有深入研究过RunLoop,非常的惭愧.刚好前一阵子负责性能优化项目,需要利用RunLoop做性能优化和性能检测,趁着这个机会深入研 ...

  9. iOS开发之Socket通信实战--Request请求数据包编码模块

    实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...

随机推荐

  1. pdnsd 解析原理

    apt install dnsmasq dnsmasq-fullvim /etc/dnsmasq.conf vim /etc/pdnsd.conf killall pdnsdpdnsd -c /etc ...

  2. samba-设定文件共享

    家里有两台电脑,一台装的是window,一台装的是ubuntu.一直想要实现两台电脑资源共享,今天终于成功实现了. smaba 具体简介就不说了,反正就知道,它可以创建一个服务器,然后让其他的电脑共享 ...

  3. 24.Node.js Stream(流)

    转自:http://www.runoob.com/nodejs/nodejs-stream.html Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...

  4. Vue前后端分离常用JS函数点(一)

    1.筛选过滤:array.filter(function(val){});  会把array中符合规则的数组元素按array里面的元素顺序保留下来. // 官方解释:按返回true或者false,把不 ...

  5. Vue router的query对象里的值的问题

    在使用 $router.push() 时,如果使用了query,则可以在跳转后从query中获取到对应的参数.如果传的是字符串自然没问题,但是如果传的其他类型的数据,在跳转之后是正常的,而跳转之后再刷 ...

  6. Bitmap-把方形图片处理为圆形

    这个是直接在网上转载的,自己验证可靠 转载自http://my.oschina.net/zhouz/blog/213164 直接贴上代码 import android.graphics.Bitmap; ...

  7. Vue 使用use、prototype自定义自己的全局组件

    使用Vue.use()写一个自己的全局组件. 目录如下: 然后在Loading.vue里面定义自己的组件模板 <template> <div v-if="loadFlag& ...

  8. nodeJS+socket.io传递消息

    服务器端 安装express,socket.io npm install express --save-dev npm install socket.io --save app.js const ex ...

  9. [React] Create component variations in React with styled-components and "extend"

    In this lesson, we extend the styles of a base button component to create multiple variations of but ...

  10. windows下安装wabt

    windows下安装wabt 安装前准备cmake.mingw环境 安装cmake 安装mingw 步骤 # 1.克隆wabt源码 git clone https://github.com/WebAs ...