How can i detect the library image is from front camera or back camera

遇到一個奇怪的問題,iOS 前鏡頭拍的照片,被旋轉了 180度。

解法如下:


Your code checks for available cameras on the device. What you need to do is read the metadata for the image after you have taken the picture, that will include info on the camera.

Use this solution to read the Exif data that comes with the image to find out which camera obtained it: Exif Data from Image

You can check the image EXIF data in the info dictionary UIImagePicker passes in it’s callback.

- (IBAction) handleTakePhoto:(UIButton *)sender {

    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:nil];

}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    __block NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];

    dispatch_async(dispatch_get_main_queue(), ^{

        NSLog(@"%@", [metadata valueForKeyPath:@"{Exif}.LensModel"]);

        [picker dismissViewControllerAnimated:YES completion:nil];

    });

}

The above snippet outputs

iPhone 6 Plus back camera 4.15mm f/2.2

You would have to parse out the “front” or “back” parts of the string.

Relying on parsing something that is parsed out of a string raises some red flags — there is probably a better and more stable way of doing it.

相關文章

寫留言

你的電子郵件位址並不會被公開。 必要欄位標記為 *