iOS 11中如何将JPEG/RAW图像数据保存至相机胶卷?无法访问处理后图像数据
Hey there! I’ve run into this exact problem when building a photo app that handles both RAW and JPEG captures, so let me share a complete, working solution using the modern iOS APIs since those old jpegPhotoDataRepresentation and dngPhotoDataRepresentation methods got deprecated in iOS 11.
Step 1: Configure Your AVCapturePhotoOutput for RAW + JPEG Capture
First, you need to set up your capture settings to enable both RAW and JPEG output. Make sure to check if the device supports RAW capture first (not all iOS devices do):
// Assuming you've already set up your AVCaptureSession and photoOutput let photoSettings = AVCapturePhotoSettings() // Enable RAW capture if available if let rawPixelFormat = photoSettings.availableRawPhotoPixelFormatTypes.first { photoSettings.isRawCaptureEnabled = true photoSettings.rawPhotoPixelFormatType = rawPixelFormat } // Configure JPEG settings photoSettings.isHighResolutionCaptureEnabled = true photoSettings.jpegQuality = .high // Trigger the capture photoOutput.capturePhoto(with: photoSettings, delegate: self)
Step 2: Process the Photo in the Delegate Method
In your didFinishProcessingPhoto delegate method, you’ll extract both the JPEG and RAW (DNG) data using the modern dataRepresentation(for:) method instead of the deprecated ones:
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { guard error == nil else { print("Photo processing error: \(error!.localizedDescription)") return } // Extract and save JPEG data if let jpegData = photo.dataRepresentation(for: .jpeg) { saveToCameraRoll(imageData: jpegData, isRaw: false) } // Extract and save RAW (DNG) data if let rawData = photo.dataRepresentation(for: .dng) { saveToCameraRoll(imageData: rawData, isRaw: true) } }
Step 3: Save to Camera Roll Using PHPhotoLibrary
You’ll use PHPhotoLibrary to save the data to the user’s camera roll. Don’t forget to request photo library access first:
private func saveToCameraRoll(imageData: Data, isRaw: Bool) { // Request photo library access PHPhotoLibrary.requestAuthorization { authorizationStatus in guard authorizationStatus == .authorized else { print("Photo library access denied. Can't save image.") return } // Perform the save operation PHPhotoLibrary.shared().performChanges({ let creationRequest = PHAssetCreationRequest.forAsset() creationRequest.addResource(with: .photo, data: imageData, options: nil) }, completionHandler: { success, error in DispatchQueue.main.async { if success { print("\(isRaw ? "RAW (DNG)" : "JPEG") image saved to camera roll successfully!") } else { print("Failed to save \(isRaw ? "RAW" : "JPEG") image: \(error?.localizedDescription ?? "Unknown error")") } } }) } }
Key Notes to Keep in Mind
- Device Compatibility: RAW capture is supported on iPhone 6s and later, iPad Pro (all models), and some other newer iOS devices. Always check
availableRawPhotoPixelFormatTypesbefore enabling RAW capture to avoid crashes. - Permissions: Don’t forget to add the required privacy entries to your
Info.plist:NSCameraUsageDescription(explain why you need camera access)NSPhotoLibraryAddUsageDescription(explain why you need to save to the photo library)
- Performance: RAW files are much larger than JPEGs, so make sure to handle memory usage appropriately, especially when capturing multiple RAW shots in a row.
内容的提问来源于stack exchange,提问作者Martin Tarrou




