求奶酪陷阱ost资源全集百度云资源

一个可以实现类似snapchat屏幕的自定义相机
一个可以实现类似snapchat屏幕的自定义相机
作者:莫名丶
资源类型:
运行环境:
这是一个简单的自定义相机,LLsimpleCamera控件可以实现类似snapchat的相机屏幕,可以轻松捕捉图像、处理位置和闪光,并隐藏与开发者相关的细节
服务热线:400-678-8266&>&&>&&>&&>&IOS 使用相机拍照和自定义拍照界面
IOS 使用相机拍照和自定义拍照界面
上传大小:88KB
Demo详细介绍了如何使用系统的相机,并且访问相册。第二个界面介绍了如何自定义拍照界面。详细讲解参见我的博客blog.csdn.net/hello_hwc
综合评分:4.3(52位用户评分)
所需积分:0
下载次数:1684
审核通过送C币
创建者:nigelyq
创建者:nigelyq
课程推荐相关知识库
上传者其他资源上传者专辑
移动开发热门标签
VIP会员动态
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
android服务器底层网络模块的设计方法
所需积分:0
剩余积分:720
您当前C币:0
可兑换下载积分:0
兑换下载分:
兑换失败,您当前C币不够,请先充值C币
消耗C币:0
你当前的下载分为234。
IOS 使用相机拍照和自定义拍照界面
会员到期时间:
剩余下载次数:
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:10029人阅读
iOS开发(475)
30分钟搞定iOS自定义相机
字数1490&阅读1126&&喜欢84
  最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以。就花时间简单研究了一下相机的自定义。
  相机属于系统硬件,这就需要我们来手动调用iPhone的相机硬件,分为以下步骤:
上午9.51.09.png
1、首先声明以下对象
#import &AVFoundation/AVFoundation.h&
@property (nonatomic, strong) AVCaptureDevice *
@property (nonatomic, strong) AVCaptureDeviceInput *
@property (nonatomic ,strong) AVCaptureStillImageOutput *imageO
@property (nonatomic, strong) AVCaptureSession *
@property (nonatomic ,strong) AVCaptureVideoPreviewLayer *previewL
2、初始化各个对象
- (void)cameraDistrict
self.device = [self cameraWithPosition:AVCaptureDevicePositionFront];
self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:nil];
self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPreset640x480;
if ([self.session canAddInput:self.input]) {
[self.session addInput:self.input];
if ([self.session canAddOutput:self.imageOutput]) {
[self.session addOutput:self.imageOutput];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
self.previewLayer.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64);
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:self.previewLayer];
[self.session startRunning];
if ([_device lockForConfiguration:nil]) {
if ([_device isFlashModeSupported:AVCaptureFlashModeAuto]) {
[_device setFlashMode:AVCaptureFlashModeAuto];
if ([_device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
[_device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
[_device unlockForConfiguration];
根据前后置位置拿到相应的摄像头:
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for ( AVCaptureDevice *device in devices )
if ( device.position == position ){
return nil;
3、拍照拿到相应图片:
- (void)photoBtnDidClick
AVCaptureConnection *conntion = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];
if (!conntion) {
NSLog(@&拍照失败!&);
[self.imageOutput captureStillImageAsynchronouslyFromConnection:conntion completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer == nil) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
self.image = [UIImage imageWithData:imageData];
[self.session stopRunning];
[self.view addSubview:self.cameraImageView];
4、保存照片到相册:
#pragma - 保存至相册
- (void)saveImageToPhotoAlbum:(UIImage*)savedImage
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
NSString *msg = nil ;
if(error != NULL){
msg = @&保存图片失败& ;
msg = @&保存图片成功& ;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@&保存图片结果提示&
message:msg
delegate:self
cancelButtonTitle:@&确定&
otherButtonTitles:nil];
[alert show];
5、前后置摄像头的切换
- (void)changeCamera{
NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
if (cameraCount & 1) {
CATransition *animation = [CATransition animation];
animation.duration = .5f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = @&oglFlip&;
AVCaptureDevice *newCamera = nil;
AVCaptureDeviceInput *newInput = nil;
AVCaptureDevicePosition position = [[_input device] position];
if (position == AVCaptureDevicePositionFront){
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
animation.subtype = kCATransitionFromLeft;
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
animation.subtype = kCATransitionFromRight;
newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
[self.previewLayer addAnimation:animation forKey:nil];
if (newInput != nil) {
[self.session beginConfiguration];
[self.session removeInput:self.input];
if ([self.session canAddInput:newInput]) {
[self.session addInput:newInput];
self.input = newI
[self.session addInput:self.input];
[self.session commitConfiguration];
} else if (error) {
NSLog(@&toggle carema failed, error = %@&, error);
6、相机的其它参数设置
- (void)focusAtPoint:(CGPoint)point{
CGSize size = self.view.bounds.size;
CGPoint focusPoint = CGPointMake( point.y /size.height ,1-point.x/size.width );
if ([self.device lockForConfiguration:&error]) {
if ([self.device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
[self.device setFocusPointOfInterest:focusPoint];
[self.device setFocusMode:AVCaptureFocusModeAutoFocus];
if ([self.device isExposureModeSupported:AVCaptureExposureModeAutoExpose ]) {
[self.device setExposurePointOfInterest:focusPoint];
[self.device setExposureMode:AVCaptureExposureModeAutoExpose];
[self.device unlockForConfiguration];
_focusView.center =
_focusView.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
_focusView.transform = CGAffineTransformMakeScale(1.25, 1.25);
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
_focusView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
_focusView.hidden = YES;
7、遇到的一些坑和解决办法
1) 前后置摄像头的切换
  前后值不能切换,各种尝试找了半天没找到有原因。后来发现我在设置图片尺寸的时候设置为1080P [self.session canSetSessionPreset: AVCaptureSessionPreset] ,前置摄像头并不支持这么大的尺寸,所以就不能切换前置摄像头。我验证了下 前置摄像头最高支持720P,720P以内可自由切换。  当然也可以在前后置摄像头切换的时候,根据前后摄像头来设置不同的尺寸,这里不在赘述。
2)焦点位置
  CGPoint focusPoint = CGPointMake( point.y /size.height ,1-point.x/size.width );
setExposurePointOfInterest:focusPoint 函数后面Point取值范围是取景框左上角(0,0)到取景框右下角(1,1)之间。官方是这么写的:
  The value of this property is a CGPoint that determines the receiver's focus point of interest, if it has one. A value of (0,0) indicates that the camera should focus on the top left corner of the image, while a value of (1,1) indicates that it should focus
on the bottom right. The default value is (0.5,0.5).
  我也试了按这个来但位置就是不对,只能按上面的写法才可以。前面是点击位置的y/PreviewLayer的高度,后面是1-点击位置的x/PreviewLayer的宽度
3)对焦和曝光
  我在设置对焦是 先设置了模式setFocusMode,后设置对焦位置,就会导致很奇怪的现象,对焦位置是你上次点击的位置。所以一定要先设置位置,再设置对焦模式。
  曝光同上
8、写在最后
  附上demo:
  常用到的基本就这么多,写的并不完善,有什么不对的,欢迎大家批评指正,共同学习。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:548489次
积分:6332
积分:6332
排名:第3585名
原创:10篇
转载:494篇
评论:72条
(10)(29)(33)(19)(16)(24)(27)(33)(33)(33)(40)(42)(38)(35)(32)(48)(15)(7)iOS-自定义修改拍照界面retake和use按钮
iOS-自定义修改拍照界面retake和use按钮
发布时间: 23:31:18
编辑:www.fx114.net
本篇文章主要介绍了"iOS-自定义修改拍照界面retake和use按钮",主要涉及到iOS-自定义修改拍照界面retake和use按钮方面的内容,对于iOS-自定义修改拍照界面retake和use按钮感兴趣的同学可以参考一下。
-(UIView *)findView:(UIView *)aView withName:(NSString *)name{ &
Class cl = [aView class]; &
NSString *desc = [cl description]; &
if ([name isEqualToString:desc]) &
return aV &
for (NSUInteger i =
0; i & [aView.subviews
count]; i++) &
UIView *subView = [aView.subviews
objectAtIndex:i]; &
& & & & subView = [self
findView:subView withName:name]; &
if (subView) &
& & & & & &
return subV &
return nil; &
-(void)addSomeElements:(UIViewController *)viewController{
UIView *PLCameraView=[self
findView:viewController.view
withName:@&PLCameraView&];
UIView *bottomBar=[self
findView:PLCameraView withName:@&PLCropOverlayBottomBar&];
UIImageView *bottomBarImageForSave = [bottomBar.subviews
objectAtIndex:<span style="color:#];
UIButton *retakeButton=[bottomBarImageForSave.subviews
objectAtIndex:<span style="color:#]; &
& & [retakeButton
setTitle:@&重拍&
forState:UIControlStateNormal];&
//左下角按钮
UIButton *useButton=[bottomBarImageForSave.subviews
objectAtIndex:<span style="color:#]; &
& & [useButton
setTitle:@&上传&
forState:UIControlStateNormal];&
//右下角按钮
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController
*)viewController animated:(BOOL)animated{
addSomeElements:viewController];
版权声明:本文为博主原创文章,未经博主允许不得转载。
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:2395人阅读
iOS开发--AVFoundation自定义相机
转自:/p/1
直插正题!
首先导入一个头文件
#import &AVFoundation/AVFoundation.h&
由于后面我们需要将拍摄好的照片写入系统相册中,所以我们在这里还需要导入一个相册需要的头文件
#import &AssetsLibrary/AssetsLibrary.h&
导入头文件后我们需要创建几个相机必须的属性
@property (nonatomic, strong) AVCaptureSession*
@property (nonatomic, strong) AVCaptureDeviceInput* videoI
@property (nonatomic, strong) AVCaptureStillImageOutput* stillImageO
@property (nonatomic, strong) AVCaptureVideoPreviewLayer* previewL
AVCaptureSession控制输入和输出设备之间的数据传递
AVCaptureDeviceInput调用所有的输入硬件。例如摄像头和麦克风
AVCaptureStillImageOutput用于输出图像
AVCaptureVideoPreviewLayer镜头捕捉到得预览图层
一个session可以管理多个输入输出设备,如下图所示
输入输出设备之间的关系(图片来自苹果官方开发文档)
接下来初始化所有对象,下面这个方法的调用我放到viewDidLoad里面调用了
- (void)initAVCaptureSession{
self.session = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setFlashMode:AVCaptureFlashModeAuto];
[device unlockForConfiguration];
self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:device error:&error];
if (error) {
NSLog(@&%@&,error);
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
[self.stillImageOutput setOutputSettings:outputSettings];
if ([self.session canAddInput:self.videoInput]) {
[self.session addInput:self.videoInput];
if ([self.session canAddOutput:self.stillImageOutput]) {
[self.session addOutput:self.stillImageOutput];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
NSLog(@&%f&,kMainScreenWidth);
self.previewLayer.frame = CGRectMake(0, 0,kMainScreenWidth, kMainScreenHeight - 64);
self.backView.layer.masksToBounds = YES;
[self.backView.layer addSublayer:self.previewLayer];
之后在viewWillAppear,viewDidDisappear方法里开启和关闭session
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
if (self.session) {
[self.session startRunning];
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:YES];
if (self.session) {
[self.session stopRunning];
到这里所有的初始化工作基本完成,运行程序可以看到镜头捕捉到得画面。接下来实现拍照按钮。
输出图像的时候需要用到AVCaptureConnection这个类,session通过AVCaptureConnection连接AVCaptureStillImageOutput进行图片输出,输入输出与connection的关系如下图
图片来自苹果官方开发文档
接下来搞一个获取设备方向的方法,再配置图片输出的时候需要使用
-(AVCaptureVideoOrientation)avOrientationForDeviceOrientation:(UIDeviceOrientation)deviceOrientation
AVCaptureVideoOrientation result = (AVCaptureVideoOrientation)deviceO
if ( deviceOrientation == UIDeviceOrientationLandscapeLeft )
result = AVCaptureVideoOrientationLandscapeRight;
else if ( deviceOrientation == UIDeviceOrientationLandscapeRight )
result = AVCaptureVideoOrientationLandscapeLeft;
下面是拍照按钮方法
- (IBAction)takePhotoButtonClick:(UIBarButtonItem *)sender {
AVCaptureConnection *stillImageConnection = [self.stillImageOutput
connectionWithMediaType:AVMediaTypeVideo];
UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation];
[stillImageConnection setVideoOrientation:avcaptureOrientation];
[stillImageConnection setVideoScaleAndCropFactor:1];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault,
imageDataSampleBuffer,
kCMAttachmentMode_ShouldPropagate);
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:jpegData metadata:(__bridge id)attachments completionBlock:^(NSURL *assetURL, NSError *error) {
至此相机的拍照功能已经完成
[stillImageConnection setVideoScaleAndCropFactor:1];这个方法是控制焦距用的暂时先固定为1,后边写手势缩放焦距的时候会修改这里照片写入相册之前需要进行旋转(我在代码里并没有进行旋转)写入相册之前需要判断用户是否允许了程序访问相册,否则程序会崩溃,包括在开启相机的时候和拍摄按钮点击的时候都需要做安全验证,验证设别是否支持拍照,用户是否允许程序访问相机。
接下来完成闪光灯
- (IBAction)flashButtonClick:(UIBarButtonItem *)sender {
NSLog(@&flashButtonClick&);
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if ([device hasFlash]) {
if (device.flashMode == AVCaptureFlashModeOff) {
device.flashMode = AVCaptureFlashModeOn;
[sender setTitle:@&flashOn&];
} else if (device.flashMode == AVCaptureFlashModeOn) {
device.flashMode = AVCaptureFlashModeAuto;
[sender setTitle:@&flashAuto&];
} else if (device.flashMode == AVCaptureFlashModeAuto) {
device.flashMode = AVCaptureFlashModeOff;
[sender setTitle:@&flashOff&];
NSLog(@&设备不支持闪光灯&);
[device unlockForConfiguration];
闪光灯的设置非常简单,只需要修改device的flashMode属性即可,这里需要注意的是,修改device时候需要先锁住,修改完成后再解锁,否则会崩溃,设置闪光灯的时候也需要做安全判断,验证设备是否支持闪光灯,有些iOS设备是没有闪光灯的,如果不做判断还是会crash掉 T_T
剩下一个小功能就是切回镜头了,方法如下
- (IBAction)switchCameraSegmentedControlClick:(UISegmentedControl *)sender {
NSLog(@&%ld&,(long)sender.selectedSegmentIndex);
AVCaptureDevicePosition desiredP
if (isUsingFrontFacingCamera){
desiredPosition = AVCaptureDevicePositionBack;
desiredPosition = AVCaptureDevicePositionFront;
for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
if ([d position] == desiredPosition) {
[self.previewLayer.session beginConfiguration];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
for (AVCaptureInput *oldInput in self.previewLayer.session.inputs) {
[[self.previewLayer session] removeInput:oldInput];
[self.previewLayer.session addInput:input];
[self.previewLayer.session commitConfiguration];
isUsingFrontFacingCamera = !isUsingFrontFacingC
isUsingFrontFacingCamera这个属性是个BOOL&#20540;变量,前面忘记写这个属性了。用于防止重复切换统一摄像头,调用这个点击方法的控件是个segement,文章最后我会附上demo地址。
最后一步就是加入手势缩放,手动调节相机焦距。
加入两个属性,并遵守这个协议&UIGestureRecognizerDelegate&
@property(nonatomic,assign)CGFloat beginGestureS
@property(nonatomic,assign)CGFloat effectiveS
这两个属性分别用于记录缩放的比例。相机支持的焦距是1.0~67.5,所以再控制器加载的时候分别给这两个属性附上一个初&#2。之后给view添加一个缩放手势,手势调用的方法如下
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer{
BOOL allTouchesAreOnThePreviewLayer = YES;
NSUInteger numTouches = [recognizer numberOfTouches],
for ( i = 0; i & numT &#43;&#43;i ) {
CGPoint location = [recognizer locationOfTouch:i inView:self.backView];
CGPoint convertedLocation = [self.previewLayer convertPoint:location fromLayer:self.previewLayer.superlayer];
if ( ! [self.previewLayer containsPoint:convertedLocation] ) {
allTouchesAreOnThePreviewLayer = NO;
if ( allTouchesAreOnThePreviewLayer ) {
self.effectiveScale = self.beginGestureScale * recognizer.scale;
if (self.effectiveScale & 1.0){
self.effectiveScale = 1.0;
NSLog(@&%f--------------&%f------------recognizerScale%f&,self.effectiveScale,self.beginGestureScale,recognizer.scale);
CGFloat maxScaleAndCropFactor = [[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor];
NSLog(@&%f&,maxScaleAndCropFactor);
if (self.effectiveScale & maxScaleAndCropFactor)
self.effectiveScale = maxScaleAndCropF
[CATransaction begin];
[CATransaction setAnimationDuration:.025];
[self.previewLayer setAffineTransform:CGAffineTransformMakeScale(self.effectiveScale, self.effectiveScale)];
[CATransaction commit];
这样之再实现一个delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] ) {
self.beginGestureScale = self.effectiveScale;
return YES;
在每次手势开始的时候把上一次实际缩放&#20540;赋给初始缩放&#20540;,如果不这么做的话你会发现每次手势开始的时候界面都会跳来跳去的(非常性感)。一个简单功能的相机基本上完成了,最后一步就是之前我们在拍照的方法里写死了一个1.0,我们还需要修改一下它,,否则虽然你看到的界面焦距改变了,但是实际拍出来的照片是没有变化的。找到拍照方法里的
[stillImageConnection setVideoScaleAndCropFactor:1.0];
[stillImageConnection setVideoScaleAndCropFactor:self.effectiveScale];
至此大功告成。
官方的演示demo里面还有个面部识别。
最后如果你想监听相机的对焦事件的话
再viewWillApper里面添加个监听
AVCaptureDevice *camDevice =[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
int flags =NSKeyValueObservingOptionNew;
[camDevice addObserver:self forKeyPath:@&adjustingFocus& options:flags context:nil];
然后实现下面方法
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {..........
最后别忘了再viewDidDisapper方法里移除监听
AVCaptureDevice*camDevice =[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[camDevice removeObserver:self forKeyPath:@&adjustingFocus&];
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:77340次
积分:1239
积分:1239
排名:千里之外
原创:24篇
转载:162篇
(3)(3)(1)(5)(3)(8)(4)(4)(3)(10)(11)(1)(3)(6)(12)(18)(14)(3)(4)(3)(4)(14)(20)(5)(2)(6)(7)(10)

我要回帖

更多关于 奶酪陷阱16百度云资源 的文章

 

随机推荐