开发者

(iphone) fast UIImage composition(merge)?

开发者 https://www.devze.com 2023-02-02 19:15 出处:网络
i\'m using the code below to merge two UIImages, wonder if there are faster way. - (UIImage*) combineImage: (UIImage*) aImage

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.siz开发者_如何学Pythone.height)];  
    [aImage drawInRect: CGRectMake(0, 0, self.size.width, self.size.height)];


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


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

精彩评论

暂无评论...
验证码 换一张
取 消