Access the Camera with iOS

Accessing the camera and photo library in swift 4

Here is the code to load image from photos & camera in iOS.

⁃ You need to create an outlet of your UIImageView

⁃ Then add a tap gesture on to image view

⁃ Then connect tap gesture with the didTapOnImageView function.

⁃ Then add the following extension to your view controller.

//MARK:- Image Picker
extension YourViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

//This is the tap gesture added on my UIImageView.
@IBAction func didTapOnImageView(sender: UITapGestureRecognizer) {
//call Alert function
self.showAlert()
}

//Show alert to selected the media source type.
private func showAlert() {

let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
self.getImage(fromSourceType: .camera)
}))
alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
self.getImage(fromSourceType: .photoLibrary)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
self.present(alert, animated: true, completion: nil)
}

//get image from source type
private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) {

//Check is source type available
if UIImagePickerController.isSourceTypeAvailable(sourceType) {

let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = sourceType
self.present(imagePickerController, animated: true, completion: nil)
}
}

//MARK:- UIImagePickerViewDelegate.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

self.dismiss(animated: true) { [weak self] in

guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
//Setting image to your image view
self?.profileImgView.image = image
}
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}

}

Note: Don't forget to add the privacy settings in info.plist

Privacy - Camera Usage Description

Privacy - Photo Library Usage Description

Request Permission for Camera and Library in iOS 10 - Info.plist

You have to add the below permission in Info.plist. More Referance

Camera :

Key       :  Privacy - Camera Usage Description   
Value : $(PRODUCT_NAME) camera use

Photo :

Key       :  Privacy - Photo Library Usage Description    
Value : $(PRODUCT_NAME) photo use

How to access camera on iOS11 home screen web app?

We have quite similar problem. So far the only workaround we were able to do is to remove the meta tag for it to be "apple-mobile-web-app-capable" and let users to open it in Safari, where everything seems to work normally.

Accessing the camera in iOS Safari on an iPhone

I posted this question on other services and eventually received a response a user named Ravavyr through a separate Discord channel. The response pointed me to this article which provided an answer.

https://leemartin.dev/hello-webrtc-on-safari-11-e8bcb5335295

It appears that iOS Safari needs some more configuration to make it work.

The CodePen linked from the original post has been updated to include the changes that make it work.

// Fix for iOS Safari from https://leemartin.dev/hello-webrtc-on-safari-11-e8bcb5335295
video.setAttribute('autoplay', '');
video.setAttribute('muted', '');
video.setAttribute('playsinline', '')

I hope this helps future readers.

Not able to Open a Camera for iOS

It could be just as the error mentions. There isn't any .builtInDualCamera option available for the device.

The type for common camera that you can try is

let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)

If you really do require the .builtInDualCamera, you can follow as the Apple document suggested which is to implement an if...else check for a fallback plan.

Building a Camera App

Is it possible to use camera permission but block Take video option in a iOS App?

I've solved it by blocking video types using input "accept" property:

<input type="file" accept="image/*" id="my_file" />

In result, iOS didn't showed the "take video" option.

Complete reference can be found at: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept

How can I access a iPhone's camera from a web page?

Currently, there is no way to directly access the phone's camera from a web page from within html5 as only native applications are given access to a device's camera. The closest you would be able to get would be to use something like Cordova to create a native application which would have access to the camera.

iOS 13 Image Capture API for accessing external camera's filesystem?

You're looking for the ImageCaptureCore framework. This is the same framework that exists on macOS for importing from SD Cards and Cameras. It is now available in iOS 13.2.

Update:

The ImageCaptureCore API is now working as of iOS 13.2.

However, be warned that as of iOS/iPadOS 13.1 Beta 3 (17A5837a) I have not been able to get it working yet (reported to Apple FB6799036). It is now listed with an asterisk on the iPadOS Features page indicating that it will be "Coming later this year".

I'm able to start an ICDeviceBrowser, but I see permissions errors when a device is connected and don't get any delegate messages. So there may be some permission or entitlement that is needed before it starts working.

Unfortunately there is no documentation or sample code (even for macOS) on Apple's developer site. But the framework does exist in the iOS 13 SDK and you can look at the header files there.

We use this framework in our macOS app and using just the headers to figure things out isn't too bad. You'd start by creating an ICDeviceBrowser (ICDeviceBrowser.h), setting its delegate, and then starting the browser:

@interface CameraManager() : NSObject <ICDeviceBrowserDelegate>
{
ICDeviceBrowser* _deviceBrowser;
}
@end

@implementation CameraManager
- (id) init
{
self = [super init];
_deviceBrowser = [[ICDeviceBrowser alloc] init];
_deviceBrowser.delegate = self;
[_deviceBrowser start];

return self;
}
...
@end

You should then start receiving delegate messages when a camera device is connected:

- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing;
- (void)deviceBrowser:(ICDeviceBrowser*)browser didRemoveDevice:(ICDevice*)removedDevice moreGoing:(BOOL)moreGoing;

When you get a didAddDevice: message you'll then want to use the ICDevice (ICDevice.h) and ICCameraDevice (ICCameraDevice.h) APIs to set a delegate and start a session. Once the session has started you'll start receiving delegate messages:

- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing
{
if ((addedDevice.type & ICDeviceTypeMaskCamera) == ICDeviceTypeCamera)
{
ICCameraDevice* camera = (ICCameraDevice *) addedDevice;
camera.delegate = self;
[camera requestOpenSession];
// probably want to save 'camera' to a member variable
}
}

You can use the delegate method:

- (void)cameraDevice:(nonnull ICCameraDevice *)camera
didAddItems:(nonnull NSArray<ICCameraItem *> *)items;

To get a list of items as they are enumerated by the API or wait for:

- (void)deviceDidBecomeReadyWithCompleteContentCatalog:(ICDevice*)device;

And then use the .contents property on the ICCameraDevice to get all of the contents.

From there you can use the ICCameraDevice to request thumbnails, metadata, and to download specific files. I'll leave that as an exercise to the reader.

As I mentioned above this doesn't seem to be working in iOS/iPadOS 13.1 Beta 3. Hopefully this will all start working soon as I'd really like to start testing it myself.

This is now working in iOS 13.2.



Related Topics



Leave a reply



Submit