2015-10-06 25 views
1

在我正在开发的应用程序中,我们捕获的照片需要具有4:3的宽高比才能最大化我们捕获的视野。直到现在我们使用AVCaptureSessionPreset640x480预设,但现在我们需要更大的分辨率。调整CaptureStillImageBracketAsynchronouslyFromConnection提供的CMSampleBufferRef大小:withSettingsArray:completionHandler:

据我所知,唯一的另外两种4:3格式是2592x1936和3264x2448。由于这些对我们的用例来说太大了,我需要一种缩小它们的方法。我研究了一些选项,但没有找到一种方式(优先选择不复制数据)以高效的方式执行此操作,而不会丢失exif数据。

vImage是我研究的内容之一,但据我所知,数据需要复制,exif数据会丢失。另一种选择是从jpegStillImageNSDataRepresentation提供的数据中创建一个UIImage,对其进行缩放并获取数据。这种方法似乎也剥离了exif数据。

这里的理想方法是直接调整缓冲区内容大小并调整照片大小。有没有人有一个想法,我会如何去做这件事?

回答

0

我结束了使用ImageIO的大小调整目的。如果有人遇到同样的问题,请留下这段代码,因为我在这方面花了太多时间。

此代码将保留exif数据,但会创建图像数据的副本。我运行了一些基准 - 这个方法的执行时间在iPhone6上约为0.05秒,使用AVCaptureSessionPresetPhoto作为原始照片的预设。

如果有人确实有更优化的解决方案,请留下评论。

- (NSData *)resizeJpgData:(NSData *)jpgData 
{ 
    CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)jpgData, NULL); 

    // Create a copy of the metadata that we'll attach to the resized image 
    NSDictionary *metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL)); 
    NSMutableDictionary *metadataAsMutable = [metadata mutableCopy]; 

    // Type of the image (e.g. public.jpeg) 
    CFStringRef UTI = CGImageSourceGetType(source); 

    NSDictionary *options = @{ (id)kCGImageSourceCreateThumbnailFromImageIfAbsent: (id)kCFBooleanTrue, 
           (id)kCGImageSourceThumbnailMaxPixelSize: @(MAX(FORMAT_WIDTH, FORMAT_HEIGHT)), 
           (id)kCGImageSourceTypeIdentifierHint: (__bridge NSString *)UTI }; 
    CGImageRef resizedImage = CGImageSourceCreateThumbnailAtIndex(source, 0, (CFDictionaryRef)options); 

    NSMutableData *destData = [NSMutableData data]; 
    CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)destData, UTI, 1, NULL); 
    if (!destination) { 
     NSLog(@"Could not create image destination"); 
    } 

    CGImageDestinationAddImage(destination, resizedImage, (__bridge CFDictionaryRef) metadataAsMutable); 

    // Tell the destination to write the image data and metadata into our data object 
    BOOL success = CGImageDestinationFinalize(destination); 
    if (!success) { 
     NSLog(@"Could not create data from image destination"); 
    } 

    if (destination) { 
     CFRelease(destination); 
    } 
    CGImageRelease(resizedImage); 
    CFRelease(source); 

    return destData; 
} 
相关问题