2つのUIImageを1つのコンテキストにマージしています。それは動作しますが、それはかなり遅く実行され、私はより高速なソリューションが必要です。私の解決策として、iPad 1GでmergeImage: withImage:
を呼び出すには約400msかかります。ここでCGContextDrawImageよりも速く2つのUIImageをマージする
は、私が何をすべきかです:
-(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;
}
そしてここ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);
}
任意の提案ですか?
それぞれの方法がどれくらい時間がかかりますか? – EmilioPelaez
drawImageの呼び出しには約400msかかり、残りのメソッドはそれぞれ10ms以下です。 –
あらかじめ組み合わせてお使いのアプリに同梱されている画像はありますか?パフォーマンスのヒットは、色空間のコンボとアルファの事前乗算のために発生する可能性が最も高いです。それらを少しでも遊んでください。 –