Merging two UIImages faster than CGContextDrawImage

I combine two UIImages in one context. It works, but it works rather slowly, and I need a faster solution. As my solution, it takes about 400 ms to call mergeImage: withImage: on the iPad 1G.

That's what I'm doing:

 -(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; } 

And here are the static methods from the ImageToolbox class:

 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); } 

Any suggestions for improving performance?

+6
source share
2 answers

I could not find a faster way to merge images. I reduced the size of the images to speed up the work.

0
source

I would definitely recommend using tools to determine which message takes the most time so you can really break it. In addition, I wrote several methods that I think should do the same with much less code, but you should write everything down the way you do to really save the settings. Here they are all the same:

 -(CGContextRef)mergeImage:(UIImage *)img1 withImage:(UIImage *)img2 { CGSize size = [ImageToolbox getScreenSize]; CGRect rect = CGRectMake(0, 0, size.width, size.height); UIGraphicsBeginImageContextWithOptions(size, YES, 1.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRenderingIntent(context, kCGRenderingIntentSaturation); [img1 drawInRect:rect]; [img2 drawInRect:rect]; UIGraphicsEndImageContext(); return context; } 

Or, if you want to get a combined image right away:

 - (UIImage *)mergeImage:(UIImage *)img1 withImage:(UIImage *)img2 { CGSize size = [ImageToolbox getScreenSize]; CGRect rect = CGRectMake(0, 0, size.width, size.height); UIGraphicsBeginImageContextWithOptions(size, YES, 1.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRenderingIntent(context, kCGRenderingIntentSaturation); [img1 drawInRect:rect]; [img2 drawInRect:rect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

I have no idea whether they will be faster or not, but I really don’t know how to speed up what you have very easily if you did not have a profile profile.

In any case, I hope this helps.

0
source

Source: https://habr.com/ru/post/916924/


All Articles