#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. crmjs区分窗口是否是高速编辑

    有时候,我们须要区分打开的窗口是否是高速编辑页面,在上面做一些逻辑处理: 窗口上面附加的js代码: function loadFrom() {     var formType = Xrm.Page. ...

  2. Leetcode47: Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 推断一个链表是不是回文的,一个比較简单的办法是把链表每一个结点的值存在vect ...

  3. WINDOWS 安装 M2Crypto for Python2.7

    WINDOWS 安装 M2Crypto for Python2.7运行环境 WIN8.1 + Python2.7 + VS2008(Microsoft Visual C++ 9.0) VS2008 可 ...

  4. linux host主机名配置

    1.查看主机名 #hostname 2.查看ip #ifconfig 2.添加主机名配置 #vi /etc/hosts 新增一行 172.23.26.195 vhost145.idmp.safe

  5. 14.NPM 常用命令

    转自:http://www.runoob.com/nodejs/nodejs-npm.html PM提供了很多命令,例如install和publish,使用npm help可查看所有命令. NPM提供 ...

  6. 转一篇对EJB理解的文章

    1. 我们不禁要问,什么是"服务集群"?什么是"企业级开发"? 既然说了EJB 是为了"服务集群"和"企业级开发",那么 ...

  7. Angularjs: 封装layDate指令

    [摘要]由于业务需要,将bootstrap-datetimepicker改成了layDate. layDate是一个较成熟且便于操作的jQuery日期插件,支持同一个视图内范围选择.封装成一个指令在多 ...

  8. linux网络防火墙-iptables基础详解

    一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防火墙 ...

  9. 数据库SQL Server2012笔记(七)——java 程序操作sql server

    1.crud(增删改查)介绍:create/retrieve/update/delete 2.JDBC介绍 1)JDBC(java database connectivity,java数据库连接) 2 ...

  10. vue 星星评分组件

    显示评分和打分组件,可现实半颗星星效果 效果图: 参数名 类型 说明 score Number 分数 ,默认0,保留一位小数 disabled Boolean 是否只读,默认false,鼠标点击可以打 ...