gpt4 book ai didi

iphone - (iphone)快速 UIImage 合成(合并)?

转载 作者:行者123 更新时间:2023-12-03 19:02:10 26 4
gpt4 key购买 nike

我正在使用下面的代码来合并两个 UIImage,
不知道是否有更快的方法。

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

最佳答案

使用 UIKit 要求您在主线程上执行此操作(最新版本的 iOS 声称可以在后台线程上工作,但在我的测试中似乎并非如此)。

一种更快(或者更灵敏)的方法是使用 Quartz 在后台线程中执行此操作,如上所述。这样做的问题是该过程实际上变成了异步。您将需要某种形式的回调。方法名称的示例可能是

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

(如果您不想使用 block 回调,它可以是符合某些协议(protocol)的委托(delegate);如果您确实想使用 block ,则需要适本地 typedef 'mergeCallbackBlock')

示例(未经测试,从工作应用程序中提取)可能如下所示:

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

关于iphone - (iphone)快速 UIImage 合成(合并)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4624246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com