//
// 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