ios自定义拍照界面和选取图片界面总结

本文总结了在iOS应用中实现自定义拍照界面和图片选择界面的方法,包括如何构建用户友好的交互体验,以及相关API的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//自定义拍照界面
- (void)photograph {
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;   // 设置委托
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerController.allowsEditing = YES;
        imagePickerController.showsCameraControls = NO;
        
        
        UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [selectButton setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        
        [selectButton setTranslatesAutoresizingMaskIntoConstraints:NO];
        
        [selectButton addTarget:self action:@selector(selectButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        [imagePickerController.view addSubview:selectButton];
        
        UIButton *cancleCameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancleCameraBtn setTitle:@"取消拍摄" forState:UIControlStateNormal];
        [cancleCameraBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
        [cancleCameraBtn addTarget:self action:@selector(cancleCameraBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [imagePickerController.view addSubview:cancleCameraBtn];
        
        NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:selectButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:imagePickerController.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        
        NSLayoutConstraint *contraintCenterX = [NSLayoutConstraint constraintWithItem:selectButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:imagePickerController.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
        
        NSLayoutConstraint *constraintRightX = [NSLayoutConstraint constraintWithItem:cancleCameraBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:imagePickerController.view attribute:NSLayoutAttributeRight multiplier:1 constant:0];
        
        NSLayoutConstraint *constraintBottomCancle = [NSLayoutConstraint constraintWithItem:cancleCameraBtn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:imagePickerController.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        
        [imagePickerController.view addConstraints:[NSArray arrayWithObjects:constraintBottom ,contraintCenterX ,constraintRightX,constraintBottomCancle,nil]];
        
        [self presentViewController:imagePickerController animated:YES completion:nil];  //需要以模态的形式展示
    }
}

- (void)selectButtonPressed:(UIButton *)sender {
    
    [imagePickerController takePicture];
}


//自定义选取图片界面

- (void)photoLibrary{
    //初始化UIImagePickerController 指定代理
    imagePickerController = [[UIImagePickerController alloc] init];
    //选择类型相机,相册还是什么
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //指定代理 因此我们要实现 UIImagePickerControllerDelegate,UINavigationControllerDelegate 协议
    imagePickerController.delegate = self;
    //不允许编辑
    imagePickerController.allowsEditing = NO;
    //显示相册
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
//选取照片结束调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    [picker dismissViewControllerAnimated:YES completion:^{}];
    imagePickerController = nil;
    [self performSelector:@selector(changeImageSize:) withObject:image];
}
//需要自定义选取图片界面
<pre name="code" class="objc">//不允许编辑
    imagePickerController.allowsEditing = NO;
//需要自定义拍照界面

<pre name="code" class="objc">imagePickerController.showsCameraControls = NO;










评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值