Share _ios for some of the basics of camera usage in IOS development

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.