Tôi đang gọi phương thức ủy nhiệm của AVFoundation để xử lý việc chụp ảnh, nhưng tôi gặp khó khăn khi chuyển đổi AVCapturePhoto mà nó tạo thành một UIImage với định hướng chính xác. Mặc dù thường trình bên dưới thành công, tôi luôn có được UIImage đúng hướng (UIImage.imageOrientation = 3). Tôi không có cách nào cung cấp định hướng khi sử dụng UIImage (dữ liệu: hình ảnh) và cố gắng sử dụng đầu tiên photo.cgImageRepresentation() ?. takeRetainedValue() cũng không giúp ích gì. Xin vui lòng giúp đỡ.Cách tạo UIImage từ AVCapturePhoto với định hướng chính xác?
Hướng hình ảnh là rất quan trọng ở đây vì hình ảnh kết quả đang được cung cấp cho luồng công việc Khung hình.
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
// capture image finished
print("Image captured.")
if let imageData = photo.fileDataRepresentation() {
if let uiImage = UIImage(data: imageData){
// do stuff to UIImage
}
}
}
UPDATE 1: Reading của Apple Photo Capture Programming Guide (hết hạn cho iOS11), tôi đã quản lý để tìm một điều tôi đã làm sai:
- Trên mỗi cuộc gọi chụp (self.capturePhotoOutput .capturePhoto) người ta phải thiết lập kết nối với đối tượng PhotoOutput và cập nhật hướng của nó để phù hợp với hướng của thiết bị tại thời điểm chụp ảnh. Để làm điều đó, tôi đã tạo ra một phần mở rộng của UIDeviceOrientation và sử dụng nó trên hàm snapPhoto() mà tôi đã tạo để gọi thường trình capture và đợi phương thức delegate didFinishProcessingPhoto được thực hiện. Tôi đã thêm một bản chụp các mã vì các phân cách mẫu mã ở đây dường như không hiển thị chúng một cách chính xác.
Cập nhật 2 Liên kết với dự án đầy đủ trên GitHub: https://github.com/agu3rra/Out-Loud
Thú vị, và có thể là một [lỗi nộp] (http://bugreport.apple.com). Liệu nó cũng có vẻ bỏ qua định hướng nếu bạn ghi dữ liệu đó vào một tệp và sử dụng trình khởi tạo 'UIImage' dựa trên tệp không? – rickster
Sau khi tôi đã thêm cuộc gọi kết nối vào thói quen snapPhoto() (CẬP NHẬT 1), nó không còn bỏ qua định hướng nữa, nhưng nó cho tôi trở lại các giá trị định hướng không chính xác trên UIImage mà tôi tạo ra. –
Giới thiệu về tiện ích mở rộng định hướng của bạn. Apple đã phát hành AVCam cập nhật. Có một phần mở rộng tương tự. 'extension UIDeviceOrientation { var videoĐăng ký: AVCaptureVideoOrientation? { tắc tự { trường hợp .portrait: trở về .portrait trường hợp .portraitUpsideDown: return .portraitUpsideDown trường hợp .landscapeLeft: trở về .landscapeRight trường hợp .landscapeRight: return .landscapeLeft mặc định: trở về con số không }} } ' –