(iphone) fast UIImage composition(merge)?

i'm using the code below to merge two UIImages, wonder if there are faster way.

- (UIImage*) combineImage: (UIImage*) aImage
{
    UIGraphicsBeginImageContext(self.size);
    [self drawInRect: CGRectMake(0, 0, self.size.width, self.size.height)];  
    [aImage drawInRect: CGRectMake(0, 0, self.size.width, self.size.height)];


    UIImage* combinedImage = UIGraphicsGetImageFromCurrentImageContext(); //                                                                                                                                                                                                  
    UIGraphicsEndImageContext();
    return combinedImage;
}

Answers


Using UIKit requires that you do it on the main thread (recent versions of iOS claim to work on background threads but this has not seemed to be the case in my tests).

A faster (or, more responsive) way to do it is to do it in a background thread using Quartz as was described. The problem with that is that the process effectively becomes asynchronous. You will need some form of callback. Example of the method name might be

- (void)generateMergedImageWith:(UIImage *)aImage callback:(mergeCallbackBlock)callback

(it can be a delegate conforming to some protocol instead, if you prefer not to use block callbacks; if you do want to use block, you need to typedef 'mergeCallbackBlock' appropriately)

Example (untested, pulled from working app) might look like:

CGImageRef firstCGImage = [self CGImage];
CGImageRef secondCGImage = [aImage CGImage];
// we presume the two images are equal size, or that the two images should be the size of self
CGSize size = [self size];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // below context presumes a number of things about the images and may need to be more aware
    CGContextRef ctx = CGBitmapContextCreate(NULL, size.width, size.height, 
                           8, size.width * 4, CGImageGetColorSpace(firstCGImage),
                           kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
    // we presume ctx succeeded; it may be NULL if it did not
    CGContextDrawImage(ctx, (CGRect){CGPointZero, size}, firstCGImage);
    CGContextDrawImage(ctx, (CGRect){CGPointZero, size}, secondCGImage);
    CGImageRef result = CGBitmapContextCreateImage(ctx);
    CGContextRelease(ctx);
    // we presume result succeeded; it is NULL if it did not succeed for some reason
    dispatc_async(dispatch_get_main_queue(), ^{
        // back on main thread we create the resulting UIImage
        UIImage *image = [[UIImage alloc] initWithCGImage:result];
        CGImageRelease(result);
        callback(image); // if a protocol, e.g. [thatObject didGenerateMergedImage:image];
        [image release];
    });
});

Need Your Help

How to you insert into MySQL database and returns its id in PHP PDO?

php mysql database pdo

Is there a way to insert into MySQL database using PHP PDO and return its id? Or I will just have to search fot its id after insertion?

Possible apache/mysql/php wrapper?

php mysql apache wrapper

I was tasked with writing a smallish program for data input and manipulation. My language of choice happens to be PHP. I have written out the program/site it all works fine is easily portable and