iOS11下AVCameraViewController.m剩余15个弃用警告疑难修复求助
Hey there! Let's tackle those three warnings one by one—they're all fixable with targeted changes to your code, plus we'll update you to modern APIs that avoid future deprecation headaches:
1. Deprecation of JPEGPhotoDataRepresentationForJPEGSampleBuffer:previewPhotoSampleBuffer:
That old AVCapturePhotoOutput method was replaced in iOS 11 with fileDataRepresentation on AVCapturePhoto objects. Even better, the captureStillImageAsynchronouslyFromConnection method you're using is itself deprecated (since iOS 10), so we'll switch to the newer capturePhotoWithSettings:delegate: API for a more robust solution.
First, make sure your view controller conforms to the required delegate protocol:
@interface CameraViewController () <AVCapturePhotoCaptureDelegate> // ... your existing properties here @end
2. Implicit self Retain in Block
To fix the implicit retain warning (and avoid potential retain cycles), we'll use a weak reference to self outside the block, then convert it to a strong reference inside—this makes the retain explicit and safe.
3. Incompatible Pointer Type Warning
Your _previewPhotoSampleBuffer is a double pointer (CMSampleBufferRef *), so you need to dereference it with * when passing it as a single-pointer parameter. That said, the modern API we're switching to will make this specific issue irrelevant.
Updated Full Code
Here's the revised code that addresses all three issues, plus fixes an extra deprecation with ALAssetsLibrary:
// Flash set to Auto for Still Capture [CameraViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]]; // Prepare modern photo settings for JPEG capture AVCapturePhotoSettings *photoSettings = [AVCapturePhotoSettings photoSettingsWithFormat:@{AVVideoCodecKey: AVVideoCodecTypeJPEG}]; photoSettings.flashMode = AVCaptureFlashModeAuto; // Use weak self to avoid retain cycles in the delegate callback __weak typeof(self) weakSelf = self; // Capture photo with the modern API [[self stillImageOutput] capturePhotoWithSettings:photoSettings delegate:self]; // Implement the required capture delegate method - (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error { if (!error) { __strong typeof(weakSelf) strongSelf = weakSelf; if (!strongSelf) return; // Get JPEG data using the non-deprecated method NSData *imageData = photo.fileDataRepresentation; UIImage *image = [[UIImage alloc] initWithData:imageData]; // Replace deprecated ALAssetsLibrary with PHPhotoLibrary (iOS 9+) PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary]; [library performChanges:^{ PHAssetCreationRequest *creationRequest = [PHAssetCreationRequest creationRequestForAsset]; [creationRequest addResourceWithType:PHAssetResourceTypePhoto data:imageData options:nil]; } completionHandler:^(BOOL success, NSError *error) { if (error) { NSLog(@"Failed to save photo: %@", error.localizedDescription); } }]; } else { NSLog(@"Photo capture failed: %@", error.localizedDescription); } }
Quick Extra Notes:
- We swapped out
ALAssetsLibrary(deprecated since iOS 9) withPHPhotoLibraryfrom the Photos framework—this eliminates another hidden deprecation warning you might have hit later. - The
capturePhotoWithSettings:delegate:method relies on the delegate callback to handle the captured photo, which is the standard pattern for modern capture workflows. - If you absolutely had to stick with the old API temporarily (not recommended), you'd fix the pointer warning by changing
_previewPhotoSampleBufferto*_previewPhotoSampleBufferin the deprecated method call—but the modern API is the better long-term fix.
内容的提问来源于stack exchange,提问作者Harry McGovern

