ios调用相册或相机上传图片

来源:互联网 发布:人工智能专业就业方向 编辑:程序博客网 时间:2024/04/29 16:52

先添加一个button用来点击进行上传,点击button触发button事件:

- (IBAction)choosePhoto:(id)sender{    UIActionSheet *choosePhotoActionSheet;  //定义一个actionsheet进行选择        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {        choosePhotoActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"choose_photo", @"")                                                             delegate:self                                                     cancelButtonTitle:NSLocalizedString(@"cancel", @"")                                                destructiveButtonTitle:nil                                                    otherButtonTitles:NSLocalizedString(@"take_photo_from_camera", @""), NSLocalizedString(@"take_photo_from_library", @""), nil];    } else {        choosePhotoActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"choose_photo", @"")                                                             delegate:self                                                     cancelButtonTitle:NSLocalizedString(@"cancel", @"")                                                destructiveButtonTitle:nil                                                    otherButtonTitles:NSLocalizedString(@"take_photo_from_library", @""), nil];    }        [choosePhotoActionSheet showInView:self.view];    [choosePhotoActionSheet release];}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 为actionsheet选择后的回调

UIImagePickerControllerSourceType的三种情况(develeper document的定义):

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {

    UIImagePickerControllerSourceTypePhotoLibrary,//用户自定义的图片库

    UIImagePickerControllerSourceTypeCamera, //通过照相机获取的图片

    UIImagePickerControllerSourceTypeSavedPhotosAlbum //系统的相片库

};


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{NSUInteger sourceType = 0;    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {        switch (buttonIndex) {            case 0:                sourceType = UIImagePickerControllerSourceTypeCamera;                break;            case 1:                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;                break;            case 2:                return;        }    } else {        if (buttonIndex == 1) {            return;        } else {            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;        }    }    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];imagePickerController.delegate = self;imagePickerController.allowsEditing = YES;    imagePickerController.sourceType = sourceType;[self presentModalViewController:imagePickerController animated:YES];}#pragma mark - UIImagePickerControllerDelegate//由picker中所获取到的info后,用于处理info的函数- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {[picker dismissModalViewControllerAnimated:YES];self.photo = [info objectForKey:UIImagePickerControllerEditedImage];  //photo为存放获取到的相片[self.photoButton setImage:self.photo forState:UIControlStateNormal]; //photobutton为存放上传相片的button}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{[self dismissModalViewControllerAnimated:YES];}


原创粉丝点击