//

//  FoodListTableViewCellB.m

//  BabyFood

//

//  Created by zhuang chaoxiao on 16/3/7.

//  Copyright © 2016年 zhuang chaoxiao. All rights reserved.

//

#import "FoodListTableViewCellB.h"

#import "NSString+Additions.h"

#import "CommData.h"

@interface FoodListTableViewCellB()

{

UIImageView * postImgView;

BOOL drawed;

}

@end

@implementation FoodListTableViewCellB

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if( self )

{

postImgView = [[UIImageView alloc]initWithFrame:CGRectZero];

[self.contentView addSubview:postImgView];

}

return self;

}

- (void)awakeFromNib {

// Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state

}

-(void)draw

{

if( drawed )

{

NSLog(@"drawed~~~");

return;

}

drawed = YES;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,70);

UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);

CGContextRef context = UIGraphicsGetCurrentContext();

[[UIColor whiteColor]set];

CGContextFillRect(context, rect);

[self.model.title drawInContext:context withPosition:CGPointMake(FOOD_CELL_TITLE_X, FOOD_CELL_TITLE_Y) andFont:FontWithSize(15) andTextColor:[UIColor blackColor] andHeight:FOOD_CELL_TITLE_H andWidth:FOOD_CELL_TITLE_W];

[self.model.detail[0][@"text"] drawInRect:CGRectMake(FOOD_CELL_DESC_X, FOOD_CELL_DESC_Y, FOOD_CELL_DESC_W, FOOD_CELL_DESC_H) withAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14],NSForegroundColorAttributeName: [UIColor grayColor]}];

//[self.model.image drawInRect:CGRectMake(FOOD_CELL_IMG_X_GAP, FOOD_CELL_IMG_Y_GAP, FOOD_CELL_IMG_W, FOOD_CELL_IMG_W)];

[[self createRoundedRectImage:self.model.image size:CGSizeMake(FOOD_CELL_IMG_W, FOOD_CELL_IMG_W)] drawInRect:CGRectMake(FOOD_CELL_IMG_X_GAP, FOOD_CELL_IMG_Y_GAP, FOOD_CELL_IMG_W, FOOD_CELL_IMG_W)];

UIImage * t = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

dispatch_async(dispatch_get_main_queue(), ^{

postImgView.frame = rect;

postImgView.image = nil;

postImgView.image = t;

});

});

}

- (void)clear{

if (!drawed) {

return;

}

NSLog(@"clear~~~");

postImgView.frame = CGRectZero;

postImgView.image = nil;

drawed = NO;

}

void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,

float ovalHeight)

{

float fw, fh;

if (ovalWidth == 0 || ovalHeight == 0) {

CGContextAddRect(context, rect);

return;

}

CGContextSaveGState(context);

CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

CGContextScaleCTM(context, ovalWidth, ovalHeight);

fw = CGRectGetWidth(rect) / ovalWidth;

fh = CGRectGetHeight(rect) / ovalHeight;

CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner

CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner

CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner

CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner

CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right

CGContextClosePath(context);

CGContextRestoreGState(context);

}

-(id) createRoundedRectImage:(UIImage*)image size:(CGSize)size

{

// the size of CGContextRef

int w = size.width;

int h = size.height;

UIImage *img = image;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

CGRect rect = CGRectMake(0, 0, w, h);

CGContextBeginPath(context);

addRoundedRectToPath(context, rect, 10, 10);

CGContextClosePath(context);

CGContextClip(context);

CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

CGImageRef imageMasked = CGBitmapContextCreateImage(context);

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

return [UIImage imageWithCGImage:imageMasked];

}

@end

UITableViewCell 自定义绘制 性能高的更多相关文章

  1. 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...

  2. Android绘制优化(一)绘制性能分析

    前言 一个优秀的应用不仅仅是要有吸引人的功能和交互,同时在性能上也有很高的要求.运行Android系统的手机,虽然配置在不断的提升,但仍旧无法和PC相比,无法做到PC那样拥有超大的内存以及高性能的CP ...

  3. iOS边练边学--自定义非等高的cell

    一.使用xib或者storyboard自定义非等高的cell实现方式差不多,这里简单介绍一下通过xib文件实现的方法 <1.1>创建一个继承自UITableViewCell的子类,比如Ch ...

  4. 自定义非等高 Cell

    1.自定义非等高 Cell介绍 1.1 代码自定义(frame) 新建一个继承自 UITableViewCell 的类. 重写 initWithStyle:reuseIdentifier: 方法. 添 ...

  5. Flutter自定义绘制(1)- 绘制基础

    CustomPainter Flutter 中实现绘制的主要是CustomPainter类. 我们一般继承这个类,来使用它: class MyPainter extends CustomPainter ...

  6. Unity 扩展属性自定义绘制

    这么晚了准备睡觉的时候,去学习了一会. 发现一个标题好奇的点进去. 居然是自定义绘制属性.  在前几天这个问题把我难住了,没想到几分钟就能解决的问题. 我花了半天时间使用反射去解决...  如果我们想 ...

  7. ##DAY12 UITableViewCell自定义

    ##DAY12 UITableViewCell自定义 #pragma mark -------自定义视图步骤--------- 自定义视图步骤: 1)在自定义cell类中,将所有cell要显示的子视图 ...

  8. OSSpinLockLock加锁机制,保证线程安全并且性能高

    在aspect_add.aspect_remove方法里面用了aspect_performLocked, 而aspect_performLocked方法用了OSSpinLockLock加锁机制,保证线 ...

  9. imageNamed和imageWithContextOfFile的区别?哪个性能高

    imageNamed性能高 1.用imageNamed的方式加载时,图片使用完毕后缓存到内存中,内存消耗多,加载速度快.即使生成的对象被 autoReleasePool释放了,这份缓存也不释放,如果图 ...

随机推荐

  1. Struts2笔记——ONGL表达式语言

    OGNL是ObjectGraphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语言. ----------- ...

  2. photoshop菜单显示不全的解决方法

    photoshop菜单显示不全? 解决方法,选择菜单 编辑->菜单,下拉菜单选择photoshop默认值

  3. 学习了初级的Python

    今天傍晚完成了Code Academy上Python的所有练习,感觉Python的原力在我身体里流淌......下面要学习一些进阶的东西.之前Zhi哥跟我说Python比较简单,我还不太信.其实早在四 ...

  4. centos使用fuse挂载NTFS

    FUSE:用户空间文件系统(Filesystem in Userspace),是Linux 中用于挂载某些网络空间,如SSH,到本地文件系统的模块.如果装的是双系统,centOS并不支持ntfs分区, ...

  5. iOS:授权用户定位NSLocationManager的使用

    请求用户批准定位: 在iOS8,要想获得用户的位置,必须经过用户批准授权 开发者可以在Info.plist中添加两个配置项 –NSLocationAlwaysUsageDescription –NSL ...

  6. mysql union和union all

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集.UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型. union 是对数据进行并集操作,不包括重复 ...

  7. 【分享】IT产业中的三大定理(一) —— 摩尔定理(Moore's Law)

    科技行业流传着很多关于比尔·盖茨的故事,其中一个是他和通用汽车公司老板之间的对话.盖茨说,如果汽车工业能够像计算机领域一样发展,那么今天,买一辆汽车只需要 25 美元,一升汽油能跑四百公里.通用汽车老 ...

  8. BZOJ 2752 高速公路(road)(线段树)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2752 题意:给出一个数列A,维护两种操作: (1)将区间[L,R]之内的所有数字增加de ...

  9. 阿里云yum源

    wget -O /etc/yum.repos.d/CentOS-Base-aliyun.repo http://mirrors.aliyun.com/repo/Centos-6.repo 参考:htt ...

  10. HDFS的基本shell操作,hadoop fs操作命令

    (1)分布式文件系统 随着数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文件,这就是分布式文件管 ...