我有一个相机会话,我正在从缓冲区中拍摄图像:
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
//rotate image 90°
ciImage = [ciImage imageByApplyingTransform:CGAffineTransformMakeRotation(-M_PI/2.0)];
}
我在图像上应用过滤器,我希望它在另一个队列上应用过滤器,它不是线程安全的,所以如果我快速拍摄图像,它会将图像混合在一起(从左到右 50/50,我认为),但我试图使其成为线程安全的,并且使用NSLock或 NSRecursiveLock 将无法工作,因为它将图像混合在一起。
dispatch_async(filterQueue, ^{
CIImage *scaleImage = [CIFilter filterWithName:@"CILanczosScaleTransform" keysAndValues:kCIInputImageKey, ciImage, @"inputScale", [NSNumber numberWithFloat:0.5], nil].outputImage;
CGImageRef cgImage = [imageContext createCGImage:scaleImage fromRect:scaleImage.extent];
[self.pictureArray addObject:[UIImage imageWithCGImage:cgImage]];
CGImageRelease(cgImage);
});
有人能帮我吗?我对如何使代码成为线程安全的知识不多
图像混合如下:http: //i.stack.imgur.com/wjrIl.png