0

Thing is, I have this custom view (3 of them) in my one cell, and it does a lot of clipping and rendering of images... I really need it to do this faster.

In my drawRect:

[super drawRect:rect];

CGContextRef contextRef = UIGraphicsGetCurrentContext();

CGFloat spacer = 1.5;
if(_type == DPClippedImageViewTypeTopLeft){
    CGContextMoveToPoint(contextRef, rect.origin.x, rect.origin.y); //top left
    CGContextAddLineToPoint(contextRef, rect.origin.x, rect.origin.y);  //top left
    CGContextAddLineToPoint(contextRef, rect.origin.x + (rect.size.width) - spacer, rect.origin.y); //top right
    CGContextAddLineToPoint(contextRef, rect.origin.x, rect.origin.y + (rect.size.height) - spacer); //bottom left
    CGContextAddLineToPoint(contextRef, rect.origin.x, rect.origin.y); //top left
}
else if(_type == DPClippedImageViewTypeBottomRight){
    CGContextMoveToPoint(contextRef, rect.origin.x + (rect.size.width), rect.origin.y + spacer); //top right
    CGContextAddLineToPoint(contextRef, rect.origin.x + (rect.size.width), rect.origin.y + spacer); //top right
    CGContextAddLineToPoint(contextRef, rect.origin.x + (rect.size.width), rect.origin.y + (rect.size.height)); //bottom right
    CGContextAddLineToPoint(contextRef, rect.origin.x + spacer, rect.origin.y + (rect.size.height)); //bottom left
    CGContextAddLineToPoint(contextRef, rect.origin.x + (rect.size.width), rect.origin.y + spacer); //top right
}
else{
    CGContextMoveToPoint(contextRef, rect.origin.x, rect.origin.y);
    CGContextAddLineToPoint(contextRef, rect.origin.x, rect.origin.y);
    CGContextAddLineToPoint(contextRef, rect.origin.x, rect.origin.y + (rect.size.height));
    CGContextAddLineToPoint(contextRef, rect.origin.x + (rect.size.width), rect.origin.y + (rect.size.height));
    CGContextAddLineToPoint(contextRef, rect.origin.x + (rect.size.width), rect.origin.y);
    CGContextAddLineToPoint(contextRef, rect.origin.x, rect.origin.y);
}

// clip context to current path
CGContextClip(contextRef);

CGContextSaveGState(contextRef);

if(_type == DPClippedImageViewTypeTopLeft){
    CGContextTranslateCTM(contextRef, -rect.size.width*2.0/3.0, -rect.size.height*3.0/5.0);
}
else if (_type == DPClippedImageViewTypeBottomRight){
    CGContextTranslateCTM(contextRef, -rect.size.width*1.0/3.0, -rect.size.height*2.0/5.0);
}

CGFloat scale = (_type != DPClippedImageViewTypeNone) ? 2.0 : 1.0;
CGContextScaleCTM(contextRef, scale, scale);

// draw image
[_image drawInRect:rect];

CGContextRestoreGState(contextRef);

Now, I have 3 of these things per cell, and even with recycling, it still needs to determine which part of the image to draw and draw it...

I really can't think of a way to speed this up :( Any help is greatly appreciated!

The result of this is 2 images. One is a right triangle with its right angle at the top left, the other is a right triangle with its right angle at the bottom right.

JP.
  • 544
  • 5
  • 20
  • Not sure if it will speed anything up but you should remove the first and last CGContextAddLineToPoint statements and add CGContextClosePath to the end of each block. Also you can remove the CGContextTranslateCTM and CGContextScaleCTM statements and adjust the rect sent to [_image drawInRect:rect] – Nick C Jun 08 '13 at 07:40
  • Did you measure what's the performance bottleneck here? You can use the Time Profiler Instrument to get a pretty good idea of this. Although drawing can be quite expensive ([especially on older retina screen devices](http://floriankugler.com/blog/2013/5/24/layer-trees-vs-flat-drawing-graphics-performance-across-ios-device-generations)), I suspect that what you're seeing is related to image decompression/scaling. Are the images the same in every cell, or are they all unique? Do they have the same size as the area you draw them in? – Florian Kugler Jun 09 '13 at 19:48

0 Answers0