gpt4 book ai didi

iphone - 从 captureStillImageAsynchronouslyFromConnection 中获取 UIImage

转载 作者:行者123 更新时间:2023-12-03 20:57:53 28 4
gpt4 key购买 nike

我正在编写一个 iPhone 应用程序,它使用 AVFoundation 来处理相机内容,并且我正在尝试将 UIImage 从相机保存到相机胶卷中。

目前它是这样做的......

[imageCaptureOutput captureStillImageAsynchronouslyFromConnection:[imageCaptureOutput.connections objectAtIndex:0]
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
if (imageDataSampleBuffer != NULL)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];

MyCameraAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

[delegate processImage:image];
}
}];

我一直在观看 WWDC 教程视频,我认为这两行(NSData...和 ​​UIImage...)是从 imageDataSampleBuffer 到 UIImage 的漫长过程。

将图像保存到库中似乎花费了太长时间。

有谁知道是否有单行转换可以从中获取 UIImage?

感谢您的帮助!

奥利弗

最佳答案

我认为在完成处理程序 block 中执行此操作可能会更有效,但你是对的,它保存到库中花费了最大的时间。

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);

CVPixelBufferLockBaseAddress(imageBuffer, 0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

if ( /*wanna save metadata on iOS4.1*/ ) {
CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
[assetsLibraryInstance writeImageToSavedPhotosAlbum:cgImage metadata:metadataDict completionBlock:^(NSURL *assetURL, NSError *error) { /*do something*/ }];
CFRelease(metadataDict);
} else {
[assetsLibraryInstance writeImageToSavedPhotosAlbum:cgImage orientation:ALAssetOrientationRight completionBlock:^(NSURL *assetURL, NSError *error) { /*do something*/ }];
// i think this is the correct orientation for Portrait, or Up if deviceOr'n is L'Left, Down if L'Right
}
CGImageRelease(cgImage);

关于iphone - 从 captureStillImageAsynchronouslyFromConnection 中获取 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3644487/

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