您的位置:首页 > 移动开发 > Objective-C

调用系统的相机和相册

2016-01-13 15:30 281 查看
    UIActionSheet *as = [[UIActionSheet alloc]initWithTitle:nil

                                                   delegate:self

                                          cancelButtonTitle:@"取消"

                                     destructiveButtonTitle:@"打开照相机"

                                          otherButtonTitles:@"从手机相册获取", nil

                         ];

    as.tag=1;
    [as showInView:self.view];

//actionSheet协议

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    switch (buttonIndex) {

        case 0:{

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

                UIImagePickerController *ipc = [[UIImagePickerController alloc]init];

                [ipc setSourceType:UIImagePickerControllerSourceTypeCamera];

                ipc.delegate = self;

                ipc.allowsEditing = YES;

                [self presentViewController:ipc animated:YES completion:nil];

            }else{

                NSLog(@"这设备没相机");

            }

        }

            break;

        case 1:{

            UIImagePickerController *ipc = [[UIImagePickerController alloc]init];

            [ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

            ipc.delegate = self;

            ipc.allowsEditing = YES;

            [self presentViewController:ipc animated:YES completion:nil];

        }

            break;

    }

    

}

//选择完成

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    

    UIImage*image=[info objectForKey:@"UIImagePickerControllerEditedImage"];

    self.sendimg=[NSMutableArray arrayWithObjects:image, nil];

    

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}

//取消选择图片

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [self dismissViewControllerAnimated:YES completion:^{

    }];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  objective-c