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

来源:互联网 发布:前台开发技术 知乎 编辑:程序博客网 时间:2024/05/16 14:47
//自定义拍照界面
- (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;






0 0
原创粉丝点击