Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLiu committed Dec 1, 2012
1 parent 834ff8a commit 7aeb345
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 21 deletions.
1 change: 1 addition & 0 deletions sample_WeiboPlus/Controllers/HomeViewController.m
Expand Up @@ -68,6 +68,7 @@ - (void)viewDidLoad
{ {
[super viewDidLoad]; [super viewDidLoad];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[Images textureBackgroundImage]]; self.tableView.backgroundColor = [UIColor colorWithPatternImage:[Images textureBackgroundImage]];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
} }


- (void)viewDidUnload - (void)viewDidUnload
Expand Down
Binary file modified sample_WeiboPlus/Resources/Images/bg-tweet-outer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sample_WeiboPlus/Resources/Images/bg-tweet-outer@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample_WeiboPlus/Views/TweetView/TweetLayer.m
Expand Up @@ -20,7 +20,7 @@ - (id)init {
self.wrapped = YES; self.wrapped = YES;
self.rasterizationScale = [[UIScreen mainScreen] scale]; self.rasterizationScale = [[UIScreen mainScreen] scale];
self.contentsScale = [[UIScreen mainScreen] scale]; self.contentsScale = [[UIScreen mainScreen] scale];
//self.drawsAsynchronously = YES; self.drawsAsynchronously = YES;


_activeLinkBackgroundLayers = [[NSMutableArray array] retain]; _activeLinkBackgroundLayers = [[NSMutableArray array] retain];
} }
Expand Down
6 changes: 6 additions & 0 deletions sample_WeiboPlus/Views/TweetView/TweetViewCell.h
Expand Up @@ -19,6 +19,12 @@
ImageDownloadReceiver *_retweetAuthorImageDownloadReceiver; ImageDownloadReceiver *_retweetAuthorImageDownloadReceiver;
UIImage *_tweetAuthorImage; UIImage *_tweetAuthorImage;
UIImage *_retweetAuthorImage; UIImage *_retweetAuthorImage;

UIImage *_drawedImage;
BOOL _isDrawing;

CALayer *_tweetAuthorProfileImageLayer;
CALayer *_retweetAuthorProfileImageLayer;
} }
@property (nonatomic, retain) TweetViewCellLayout *layout; @property (nonatomic, retain) TweetViewCellLayout *layout;


Expand Down
115 changes: 96 additions & 19 deletions sample_WeiboPlus/Views/TweetView/TweetViewCell.m
Expand Up @@ -13,15 +13,18 @@ @interface TweetViewCell() {


@property (nonatomic, retain) UIImage *tweetAuthorImage; @property (nonatomic, retain) UIImage *tweetAuthorImage;
@property (nonatomic, retain) UIImage *retweetAuthorImage; @property (nonatomic, retain) UIImage *retweetAuthorImage;
@property (nonatomic, retain) UIImage *drawedImage;


@end @end


@implementation TweetViewCell @implementation TweetViewCell
@synthesize layout = _layout; @synthesize layout = _layout;
@synthesize drawedImage = _drawedImage;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) { if (self) {
contentView.backgroundColor = [UIColor whiteColor];
_downloader = [ImageDownloader profileImagesDownloader]; _downloader = [ImageDownloader profileImagesDownloader];
_tweetAuthorImageDownloadReceiver = [[ImageDownloadReceiver alloc]init]; _tweetAuthorImageDownloadReceiver = [[ImageDownloadReceiver alloc]init];
_tweetAuthorImageDownloadReceiver.completionBlock = ^(NSData *imageData, NSString *url, NSError *error) { _tweetAuthorImageDownloadReceiver.completionBlock = ^(NSData *imageData, NSString *url, NSError *error) {
Expand All @@ -33,6 +36,13 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
}; };
_tweetAuthorImage = nil; _tweetAuthorImage = nil;
_retweetAuthorImage = nil; _retweetAuthorImage = nil;


_tweetAuthorProfileImageLayer = [self profileImageLayer];
_retweetAuthorProfileImageLayer = [self profileImageLayer];
[contentView.layer addSublayer:_tweetAuthorProfileImageLayer];
[contentView.layer addSublayer:_retweetAuthorProfileImageLayer];

} }
return self; return self;
} }
Expand All @@ -45,20 +55,29 @@ - (void)dealloc {
[_retweetAuthorImageDownloadReceiver release]; [_retweetAuthorImageDownloadReceiver release];
[_tweetAuthorImage release]; [_tweetAuthorImage release];
[_retweetAuthorImage release]; [_retweetAuthorImage release];
[_drawedImage release];


[super dealloc]; [super dealloc];
} }


- (CALayer *)profileImageLayer { - (CALayer *)profileImageLayer {
CALayer *layer = [CALayer layer]; CALayer *layer = [CALayer layer];
layer.shouldRasterize = YES; layer.shouldRasterize = YES;
//layer.frame = CGRectMake(10, 10, 34, 34); layer.frame = CGRectMake(10, 10, 34, 34);
layer.masksToBounds = YES; //layer.masksToBounds = YES;
layer.rasterizationScale = [[UIScreen mainScreen] scale]; layer.rasterizationScale = [[UIScreen mainScreen] scale];
layer.drawsAsynchronously = YES; layer.drawsAsynchronously = YES;
NSDictionary *actions = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNull null], @"contents", nil];
layer.actions = actions;
[actions release];
return layer; return layer;
} }


- (void)resetProfileImageLayers {
_tweetAuthorProfileImageLayer.contents = (id)[self profileHolderImage].CGImage;
_retweetAuthorProfileImageLayer.contents = (id)[self profileHolderImage].CGImage;
}

- (void)setLayout:(TweetViewCellLayout *)layout { - (void)setLayout:(TweetViewCellLayout *)layout {
if (_layout != layout) { if (_layout != layout) {
[_downloader removeDelegate:_tweetAuthorImageDownloadReceiver forURL:_layout.status.user.profileImageUrl]; [_downloader removeDelegate:_tweetAuthorImageDownloadReceiver forURL:_layout.status.user.profileImageUrl];
Expand All @@ -67,8 +86,18 @@ - (void)setLayout:(TweetViewCellLayout *)layout {
} }
self.tweetAuthorImage = nil; self.tweetAuthorImage = nil;
self.retweetAuthorImage = nil; self.retweetAuthorImage = nil;
_isDrawing = NO;
self.drawedImage = nil;
[self performSelectorInBackground:@selector(resetProfileImageLayers) withObject:nil];
[_layout release]; [_layout release];
_layout = [layout retain]; _layout = [layout retain];

[CATransaction begin];
[CATransaction setAnimationDuration:0];
_tweetAuthorProfileImageLayer.frame = _layout.tweetAuthorProfileImageRect;
_retweetAuthorProfileImageLayer.frame = _layout.retweetAuthorProfileImageRect;
[CATransaction commit];

[_downloader activeRequest:_layout.status.user.profileImageUrl delegate:_tweetAuthorImageDownloadReceiver]; [_downloader activeRequest:_layout.status.user.profileImageUrl delegate:_tweetAuthorImageDownloadReceiver];
if (_layout.status.retweetedStatus) { if (_layout.status.retweetedStatus) {
[_downloader activeRequest:_layout.status.retweetedStatus.user.profileImageUrl delegate:_retweetAuthorImageDownloadReceiver]; [_downloader activeRequest:_layout.status.retweetedStatus.user.profileImageUrl delegate:_retweetAuthorImageDownloadReceiver];
Expand All @@ -82,9 +111,9 @@ - (void)setLayout:(TweetViewCellLayout *)layout {
- (UIImage *)processImageData:(NSData *)imageData rect:(CGRect)rect { - (UIImage *)processImageData:(NSData *)imageData rect:(CGRect)rect {
UIImage *image = [UIImage imageWithData:imageData]; UIImage *image = [UIImage imageWithData:imageData];
if (image) { if (image) {
float scale = [[UIScreen mainScreen] scale]; CGRect bounds = rect;
CGRect bounds = CGRectMake(0, 0, rect.size.width * scale, rect.size.height * scale); bounds.origin = CGPointZero;
UIGraphicsBeginImageContext(bounds.size); UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 0);
//CGContextRef c = UIGraphicsGetCurrentContext(); //CGContextRef c = UIGraphicsGetCurrentContext();
//CGContextScaleCTM(c, scale, scale); //CGContextScaleCTM(c, scale, scale);


Expand All @@ -102,9 +131,9 @@ - (UIImage *)profileHolderImage {
UIImage *image = [Images profilePlaceholderOverWhiteImage]; UIImage *image = [Images profilePlaceholderOverWhiteImage];
if (image) { if (image) {
CGRect rect = _layout.tweetAuthorProfileImageRect; CGRect rect = _layout.tweetAuthorProfileImageRect;
float scale = [[UIScreen mainScreen] scale]; CGRect bounds = rect;
CGRect bounds = CGRectMake(0, 0, rect.size.width * scale, rect.size.height * scale); bounds.origin = CGPointZero;
UIGraphicsBeginImageContext(bounds.size); UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);
//CGContextRef c = UIGraphicsGetCurrentContext(); //CGContextRef c = UIGraphicsGetCurrentContext();
//CGContextScaleCTM(c, scale, scale); //CGContextScaleCTM(c, scale, scale);


Expand All @@ -119,14 +148,32 @@ - (UIImage *)profileHolderImage {


} }


- (void)displayProfileImages {
if (!_isDrawing) {
if (self.tweetAuthorImage) {
[_tweetAuthorProfileImageLayer performSelectorInBackground:@selector(setContents:) withObject:(id)self.tweetAuthorImage.CGImage];
}
if (self.retweetAuthorImage) {
[_retweetAuthorProfileImageLayer performSelectorInBackground:@selector(setContents:) withObject:(id)self.retweetAuthorImage.CGImage];
}
}

}

- (void)processTweetAuthorImageData:(NSData *)imageData { - (void)processTweetAuthorImageData:(NSData *)imageData {
self.tweetAuthorImage = [self processImageData:imageData rect:_layout.tweetAuthorProfileImageRect]; self.tweetAuthorImage = [self processImageData:imageData rect:_layout.tweetAuthorProfileImageRect];
[self setNeedsDisplayInRect:_layout.tweetAuthorProfileImageRect]; //_tweetAuthorProfileImageLayer.contents = (id)self.tweetAuthorImage.CGImage;
[self displayProfileImages];
//self.drawedImage = nil;
//[self setNeedsDisplayInRect:_layout.tweetAuthorProfileImageRect];
} }


- (void)processRetweetAuthorImageData:(NSData *)imageData { - (void)processRetweetAuthorImageData:(NSData *)imageData {
self.retweetAuthorImage = [self processImageData:imageData rect:_layout.retweetAuthorProfileImageRect]; self.retweetAuthorImage = [self processImageData:imageData rect:_layout.retweetAuthorProfileImageRect];
[self setNeedsDisplayInRect:_layout.retweetAuthorProfileImageRect]; //_retweetAuthorProfileImageLayer.contents = (id)self.retweetAuthorImage.CGImage;
[self displayProfileImages];
//self.drawedImage = nil;
//[self setNeedsDisplayInRect:_layout.retweetAuthorProfileImageRect];
} }


- (void)receiver:(ImageDownloadReceiver *)receiver didDownloadWithImageData:(NSData *)imageData - (void)receiver:(ImageDownloadReceiver *)receiver didDownloadWithImageData:(NSData *)imageData
Expand All @@ -144,41 +191,71 @@ - (void)receiver:(ImageDownloadReceiver *)receiver didDownloadWithImageData:(NSD
} }
} }


- (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted { - (void)drawOnImage {
CGContextRef context = UIGraphicsGetCurrentContext(); if (!_isDrawing) {
_isDrawing = YES;
UIImage *image = nil;
CGRect bounds = self.bounds;
UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawContentInContext:context];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.drawedImage = image;
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
_isDrawing = NO;
[self displayProfileImages];
}
}

- (void)drawContentInContext:(CGContextRef)context {
[[Images tweetOuterBackgroundImage]drawInRect:self.bounds]; [[Images tweetOuterBackgroundImage]drawInRect:self.bounds];
if (_layout.status.retweetedStatus) { if (_layout.status.retweetedStatus) {
[[Images tweetInnerBackgroundImage] drawInRect:_layout.retweetRect]; [[Images tweetInnerBackgroundImage] drawInRect:_layout.retweetRect];
} }
CGContextSaveGState(context); CGContextSaveGState(context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextSetTextMatrix(context, CGAffineTransformIdentity);


CGContextTranslateCTM(context, 0.0f, rect.size.height); CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f); CGContextScaleCTM(context, 1.0f, -1.0f);

[_layout.tweetDocument drawTextInRect:self.bounds textRect:_layout.tweetTextRect context:context]; [_layout.tweetDocument drawTextInRect:self.bounds textRect:_layout.tweetTextRect context:context];
[_layout.tweetAuthorDocument drawTextInRect:self.bounds textRect:_layout.tweetAuthorTextRect context:context]; [_layout.tweetAuthorDocument drawTextInRect:self.bounds textRect:_layout.tweetAuthorTextRect context:context];
if (_layout.status.retweetedStatus) { if (_layout.status.retweetedStatus) {
[_layout.retweetDocument drawTextInRect:self.bounds textRect:_layout.retweetTextRect context:context]; [_layout.retweetDocument drawTextInRect:self.bounds textRect:_layout.retweetTextRect context:context];
[_layout.retweetAuthorDocument drawTextInRect:self.bounds textRect:_layout.retweetAuthorTextRect context:context]; [_layout.retweetAuthorDocument drawTextInRect:self.bounds textRect:_layout.retweetAuthorTextRect context:context];
} }
CGContextRestoreGState(context); CGContextRestoreGState(context);

CGContextSaveGState(context); CGContextSaveGState(context);
CGContextSetRGBFillColor(context, 187/255.f, 187/255.f, 187/255.f, 1.f); CGContextSetRGBFillColor(context, 187/255.f, 187/255.f, 187/255.f, 1.f);


NSString *timeText = [NSString stringWithFormat:@"%@%@", [_layout.status statusTimeString], [_layout.status source]]; NSString *timeText = [NSString stringWithFormat:@"%@%@", [_layout.status statusTimeString], [_layout.status source]];
[timeText drawInRect:_layout.tweetTimeTextRect withFont:[Fonts statusTimeFont]]; [timeText drawInRect:_layout.tweetTimeTextRect withFont:[Fonts statusTimeFont]];
UIImage *profileImage = self.tweetAuthorImage ? self.tweetAuthorImage : [self profileHolderImage]; //UIImage *profileImage = self.tweetAuthorImage ? self.tweetAuthorImage : [self profileHolderImage];
[profileImage drawInRect:_layout.tweetAuthorProfileImageRect]; //[profileImage drawInRect:_layout.tweetAuthorProfileImageRect];
if (_layout.status.retweetedStatus) { if (_layout.status.retweetedStatus) {
timeText = [NSString stringWithFormat:@"%@%@", [_layout.status.retweetedStatus statusTimeString], [_layout.status.retweetedStatus source]]; timeText = [NSString stringWithFormat:@"%@%@", [_layout.status.retweetedStatus statusTimeString], [_layout.status.retweetedStatus source]];
[timeText drawInRect:_layout.retweetTimeTextRect withFont:[Fonts statusTimeFont]]; [timeText drawInRect:_layout.retweetTimeTextRect withFont:[Fonts statusTimeFont]];
profileImage = self.retweetAuthorImage ? self.retweetAuthorImage : [self profileHolderImage]; //profileImage = self.retweetAuthorImage ? self.retweetAuthorImage : [self profileHolderImage];
[profileImage drawInRect:_layout.retweetAuthorProfileImageRect]; //[profileImage drawInRect:_layout.retweetAuthorProfileImageRect];
} }
CGContextRestoreGState(context); CGContextRestoreGState(context);
} }


- (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted {
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawContentInContext:context];
/*
[[Images tweetOuterBackgroundImage]drawInRect:self.bounds];
if (self.drawedImage) {
[self.drawedImage drawInRect:self.bounds];
}
else {
[self performSelectorInBackground:@selector(drawOnImage) withObject:nil];
}
*/
}



@end @end
40 changes: 40 additions & 0 deletions sample_WeiboPlus/Views/TweetView/TweetViewCell1.h
@@ -0,0 +1,40 @@
//
// TweetViewCell1.h
// WeiboPlus
//
// Created by junmin liu on 12-11-21.
// Copyright (c) 2012年 idfsoft. All rights reserved.
//

#import "ABTableViewCell.h"
#import "TweetViewCellLayout.h"
#import "ImageDownloader.h"
#import "ImageDownloadReceiver.h"
#import "Images.h"
#import "TweetLayer.h"


@interface TweetViewCell1 : ABTableViewCell {
TweetViewCellLayout *_layout;
ImageDownloader *_downloader;
ImageDownloadReceiver *_tweetAuthorImageDownloadReceiver;
ImageDownloadReceiver *_retweetAuthorImageDownloadReceiver;
UIImage *_tweetAuthorImage;
UIImage *_retweetAuthorImage;


CALayer *_tweetAuthorProfileImageLayer;
CALayer *_retweetAuthorProfileImageLayer;

TweetLayer *_tweetTextLayer;
TweetLayer *_retweetTextLayer;
TweetLayer *_tweetAuthorLayer;
TweetLayer *_retweetAuthorLayer;
CATextLayer *_tweetTimeLayer;
CATextLayer *_retweetTimeLayer;
}

@property (nonatomic, retain) TweetViewCellLayout *layout;


@end

0 comments on commit 7aeb345

Please sign in to comment.