标签:
// ImageView.h
#import
@interface ImageView : UIView {
UIImage *image;
}
@property (retain, nonatomic) UIImage *image;
@end
// ImageView.m
#include
#import "ImageView.h"
@implementation ImageView
#define LABEL_TAG 1
static const CGRect imageRect = {{0, 0}, {100, 100}};
static const CGPoint imagePoint = {0, 0};
@synthesize image;
- (void)awakeFromNib {
if (!self.image) {
self.image = [UIImage imageNamed:@"random.jpg"];
}
}
- (void)drawRect:(CGRect)rect {
if (CGRectEqualToRect(rect, imageRect)) {
uint64_t start = mach_absolute_time();
[image drawAtPoint:imagePoint];
uint64_t drawTime = mach_absolute_time() - start;
NSString *text = [[NSString alloc] initWithFormat:@"%ld", drawTime];
UILabel *label = (UILabel *)[self viewWithTag:LABEL_TAG];
label.text = text;
[text release];
}
}
- (void)dealloc {
[super dealloc];
[image release];
}
@end
static const CGSize imageSize = {100, 100};
- (void)awakeFromNib {
if (!self.image) {
self.image = [UIImage imageNamed:@"random.jpg"];
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0);
else
UIGraphicsBeginImageContext(imageSize);
[image drawInRect:imageRect];
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
static CGImageRef imageRef;
imageRef = self.image.CGImage;
最后在drawRect:中绘制:CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, imageRect, imageRef);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, 100);
CGContextScaleCTM(context, 1, -1);
CGContextDrawImage(context, imageRect, imageRef);
标签:
原文地址:http://www.cnblogs.com/allanliu/p/4254063.html