ios中从相册:相机中获取图片信息

ios中从相册/相机中获取图片信息

从相册中获取图片的信息

UIImagePickerController *imgPickView = [[UIImagePickerController alloc] init];//获取相册的类
    imgPickView.delegate = self;//设置delegate
    imgPickView.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//设置获取相册中的图片的方法
    imgPickView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;//设置显示出这个视图的方式
    imgPickView.allowsEditing = YES;//设置这个视图是可以被编辑

    [self presentViewController:imgPickView animated:YES completion:nil];//将这个视图推入view中

从照相机中获取图片信息

//从摄像头中获取图片(和从相片中获取信息,差不多)
    UIImagePickerController *imgpickView = [[UIImagePickerController alloc] init];
    imgpickView.delegate = self;
    imgpickView.sourceType = UIImagePickerControllerSourceTypeCamera;//将这个类型设置为照相机中获取信息
    imgpickView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imgpickView.allowsEditing = YES;

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

下面是几个属性是modalTransitionStyle中可以选择的集中属性类型

     ● UIModalTransitionStyleCoverVertical:画面从下向上徐徐弹出,关闭时向下隐

          藏(默认方式)。

      ● UIModalTransitionStyleFlipHorizontal:从前一个画面的后方,以水平旋转的方

          式显示后一画面。

      ● UIModalTransitionStyleCrossDissolve:前一画面逐渐消失的同时,后一画面逐渐显示。

设置完了这个view之后,我们还需要设置一个delegate中的函数

#pragma mark- UIImagePickerControllerDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];//获取图片信息
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)//如果是照相功能的话,还需要保存图片
    {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }

    //设置路径,储存图片,(这个步骤可有可无)
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *pathImg = [path stringByAppendingString:@"/img.img"];
    NSLog(@"%@", pathImg);
    NSData *imgData = UIImagePNGRepresentation(image);
    [imgData writeToFile:pathImg atomically:YES];

    [self dismissViewControllerAnimated:YES completion:nil];
    }

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //按取消键,进行的操作
    [self dismissViewControllerAnimated:YES completion:nil];
    }

你可能感兴趣的:(相机,获取图片)