Tôi đang hợp nhất hai UIImages vào một ngữ cảnh. Nó hoạt động, nhưng nó thực hiện khá chậm và tôi đang cần một giải pháp nhanh hơn. Theo giải pháp của tôi là phải mất khoảng 400ms để thực hiện cuộc gọi mergeImage: withImage:
trên iPad 1G.Hợp nhất hai UIImages nhanh hơn CGContextDrawImage
Dưới đây là những gì tôi làm:
-(CGContextRef)mergeImage:(UIImage*)img1 withImage:(UIImage*)img2
{
CGSize size = [ImageToolbox getScreenSize];
CGContextRef context = [ImageToolbox createARGBBitmapContextFromImageSize:CGSizeMake(size.width, size.height)];
CGContextSetRenderingIntent(context, kCGRenderingIntentSaturation);
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), img1.CGImage);
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), img2.CGImage);
return context;
}
Và đây là những phương pháp tĩnh từ lớp ImageToolbox:
static CGRect screenRect;
+ (CGContextRef)createARGBBitmapContextFromImageSize:(CGSize)imageSize
{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
size_t pixelsWide = imageSize.width;
size_t pixelsHigh = imageSize.height;
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
bitmapData = malloc(bitmapByteCount);
if (bitmapData == NULL)
{
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease(colorSpace);
return NULL;
}
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
free (bitmapData);
fprintf (stderr, "Context not created!");
}
CGColorSpaceRelease(colorSpace);
return context;
}
+(CGSize)getScreenSize
{
if (screenRect.size.width == 0 && screenRect.size.height == 0)
{
screenRect = [[UIScreen mainScreen] bounds];
}
return CGSizeMake(screenRect.size.height, screenRect.size.width-20);
}
Mọi góp ý để nâng cao hiệu suất?
Bạn có thể đo lường mỗi phương pháp mất bao lâu? – EmilioPelaez
Các cuộc gọi drawImage mất khoảng 400 ms, phần còn lại của các phương pháp dưới 10ms mỗi. –
Những hình ảnh này mà bạn có thể kết hợp trước và giao hàng với ứng dụng của mình không? Hiệu suất hit rất có thể là do một số combo của không gian màu của bạn và premultiplying alpha. Chơi xung quanh với những người một chút. –