[英]Optimizing Quartz2D performance on touch events
我正在做的是每次触摸事件我都是从unsigned char *创建一个图像。 这是我的功能
-(void)paint:(ImageWarper::WarpedImage *)warpedImg isCircleRequired:(bool)doDrawCircle atPoint:(CGPoint)pt{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(!mWarper)
return;
unsigned char *pixelData = warpedImg->Image.Data;
int imageHeight = warpedImg->Image.Height;
int scanWidth = warpedImg->Image.ScanWidth;
int imageWidth = warpedImg->Image.Width;
CGDataProviderRef provider = CGDataProviderCreateWithData(
NULL,
pixelData,
imageHeight * scanWidth,
(CGDataProviderReleaseDataCallback)&freeRawData);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
int bytesPerPixel = warpedImg->Image.Bpp;
CGImageRef imageRef = CGImageCreate(imageWidth,
imageHeight,
BitsPerComponent,
bytesPerPixel * BitsPerComponent,
scanWidth,
colorSpaceRef,
bitmapInfo,
provider,
NULL,
YES,
renderingIntent);
if(!imageRef)
return;
UIImage *uiImage = [UIImage imageWithCGImage:imageRef];
imgScrollView.imgView.image = uiImage;
UIGraphicsBeginImageContext(mbmpImage.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 1.5);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
[mbmpImage drawInRect:CGRectMake(0, 0, mbmpImage.size.width, mbmpImage.size.height)];
[uiImage drawInRect:CGRectMake(warpedImg->Position.X, warpedImg->Position.Y, warpedImg->Image.Width, warpedImg->Image.Height)];
if(doDrawCircle){
[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
CGContextStrokeEllipseInRect(ctx,CGRectMake(pt.x - mRadius, pt.y - mRadius, mRadius*2, mRadius*2));
}
else{
[mbmpImage release];
mbmpImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
}
UIImage * resultingImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
imgScrollView.imgView.image = resultingImage ;
if(!doDrawCircle) {
[mbmpImage release];
mbmpImage = [resultingImage retain];
}
if(doDrawCircle){
CGPoint pt2 = [self.view convertPoint:pt fromView:imgScrollView];
imgPreview.hidden = NO;
[self addText:mbmpImage text: @"+" atPoint:pt atRect:(warpedImg->Position.X, warpedImg->Position.Y, warpedImg->Image.Width, warpedImg->Image.Height)];
}
else{
[popoverController dismissPopoverAnimated:YES];
}
[resultingImage release];
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
CGImageRelease(imageRef);
[pool drain];
}
该函数调用如下:
NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSValue valueWithPointer:wp1],@"warper", @"YES",@"isCircleRequired",
[NSValue valueWithCGPoint:location],@"point",nil];
[self performSelector:@selector(paintDic:) withObject:d];
[d release];
wp1 = nil;
-(void)paintDic:(NSMutableDictionary *)dictionary{
ImageWarper::WarpedImage *wp1 = (ImageWarper::WarpedImage *)[[dictionary objectForKey:@"warper"] pointerValue];
[self paint:wp1
isCircleRequired:[dictionary objectForKey:@"isCircleRequired"]
atPoint:[[dictionary objectForKey:@"point"] CGPointValue]];
}
我正在使用以下回调函数来释放数据。
void freeRawData(void *info, const void *data, size_t size) {
data = nil;
}
任何人都可以帮助我如何优化速度。 它在新款iphone上工作正常,但不是旧款iphone。
我在执行选择器中调用绘制,以便在完成图像创建时不会执行执行。 此外,任何帮助直接从原始数据创建图像,即unsigned char *将不胜感激。
在尝试任何优化之前,请先测量您的时间。 仪器的性能工具将在这里为您提供帮助。
在我看到的代码中,我看到了一些真正不需要的东西:
static
变量中可能会缩短一段时间。 UIImage
而是使用CGContextDrawImage
可能会节省你很多时间。 dispatch_async
而不是performSelector:withObject:
- 这样您就不需要包装和解包非对象类型。 但就像我说的:
在执行任何优化之前测量您的应用程序!
包括这些措施,我还减少了执行此代码的运行循环次数。 这项措施大大提高了我的应用程序的性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.