In some applications, we need to use a camera for the iOS device to take pictures, videos. and choose the pictures or videos we need from the album.
For iOS camera and photo album applications, you can use the Uiimagepickercontroller class to complete the control.
Uiimagepickercontroller This class can provide you with photographic functions, as well as pictures, video browsing functions.
Check that the hardware is installed with a webcam or allow you to manipulate albums
Copy Code code as follows:
#pragma mark-camera and album-related public classes
Determine if the device has a camera
-(BOOL) iscameraavailable{
return [Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera];
}
Whether the front camera is available
-(BOOL) isfrontcameraavailable{
return [Uiimagepickercontrolleriscameradeviceavailable:uiimagepickercontrollercameradevicefront];
}
If the rear camera is available
-(BOOL) isrearcameraavailable{
return [Uiimagepickercontrolleriscameradeviceavailable:uiimagepickercontrollercameradevicerear];
}
Call camera
Copy Code code as follows:
-(BOOL) Hasmultiplecameras {
Nsarray *devices = [Avcapturedevice deviceswithmediatype:avmediatypevideo];
if (Devices!= nil && [devices count] > 1) return YES;
return NO;
}
-(Avcapturedevice *) Camerawithposition: (avcapturedeviceposition) position
{
Nsarray *devices = [Avcapturedevice deviceswithmediatype:avmediatypevideo];
For (Avcapturedevice *device in devices)
if (device.position = = position)
return device;
return nil;
}
-(void) Swapfrontandbackcameras {
Check for available cameras!
if (![ Self Hasmultiplecameras]) return;
Assumes session is running
Nsarray *inputs = self.captureSession.inputs; should be one value!
For (Avcapturedeviceinput *capturedeviceinput in inputs) {
Avcapturedevice *device = Capturedeviceinput.device;
if ([device Hasmediatype:avmediatypevideo]) {
Avcapturedeviceposition position = device.position;
Avcapturedevice *newcamera = nil;
Avcapturedeviceinput *newinput = nil;
if (position = = Avcapturedevicepositionfront)
Newcamera = [self camerawithposition:avcapturedevicepositionback];
Else
Newcamera = [self camerawithposition:avcapturedevicepositionfront];
[Self initializecapturedevice:newcamera];
Newinput = [Avcapturedeviceinput deviceinputwithdevice:newcamera error:nil];
Beginconfiguration ensures that pending changes are not applied immediately
[Self.capturesession beginconfiguration];
[Self.capturesession Removeinput:capturedeviceinput]; Remove current
[Self.capturesession Addinput:newinput]; Add New
Changes take effect once the outermost commitconfiguration is invoked.
[Self.capturesession commitconfiguration];
break;
}
}
}
The code above is to use a front camera and a rear-facing camera!