来自Apple的AVCameraViewController.m示例代码更新到最新的iOS 11警告

时间:2018-05-16 08:39:49

标签: ios objective-c

我已经在这方面工作了一个星期,慢慢地删除了由现在几乎正常工作的代码中的Objective-C代码的Depreciations引起的警告和错误。我低至15折旧警告。

这段代码对我来说特别困难。它甚至还会为iOS 11.0返回Depreciations。我的意思是说......

1. JPEGPhotoDataRepresentationForJPEGSampleBuffer:previewPhotoSampleBuffer:' is deprecated: first deprecated in iOS 11.0 - Use -[AVCapturePhoto fileDataRepresentation] instead.

2. Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior
Insert 'self->'
3. Incompatible pointer types sending 'CMSampleBufferRef *' (aka 'struct opaqueCMSampleBuffer **') to parameter of type 'CMSampleBufferRef _Nullable' (aka 'struct opaqueCMSampleBuffer *'); dereference with *
Replace '_previewPhotoSampleBuffer' with '*(_previewPhotoSampleBuffer)'




        // Flash set to Auto for Still Capture
    [CameraViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];

    // Capture a still image.
    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

        if (imageDataSampleBuffer)
        {    
            NSData *imageData = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:imageDataSampleBuffer previewPhotoSampleBuffer:_previewPhotoSampleBuffer];

            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil];
        }
    }];
});

1 个答案:

答案 0 :(得分:1)

  1. 由于对图像的HEIC编解码器的新支持,在iOS 11中不推荐使用JPEGPhotoDataRepresentationForJPEGSampleBuffer:previewPhotoSampleBuffer:方法。如警告所示,您应该使用-[AVCapturePhoto fileDataRepresentation]代替。如果您确实需要预览图像,请使用AVCapturePhoto.previewPixelBuffer属性。

  2. 此警告是因为作为完成处理程序传递给captureStillImageAsynchronouslyFromConnection:connectionWithMediaType: completionHandler:的块使用_previewPhotoSampleBuffer,它(可能)是此代码所在类的实例变量。当您访问实例时变量,你实际做的是self->_previewPhotoSampleBuffer。因为需要self,所以块会捕获它,可能会创建一个保留周期。编译器会警告您,因为如果没有明确使用self,您就不太可能注意到自己。您可以通过执行self->_previewPhotoSampleBuffer来消除警告,但我认为修复警告1的更改无论如何都会删除此代码。

  3. _previewPhotoSampleBuffer似乎已被声明为CMSampleBufferRef *(换言之,指向CMSampleBufferRef的指针)。但是,CMSampleBufferRef已经是指针。它是opaqueCMSampleBuffer *的typedef。所以你要将指针传递给指向一个需要单个指针的方法的指针,即一个CMSampleBufferRef。您需要在声明_previewPhotoSampleBuffer的任何地方修复此问题。