我是靠谱客的博主 外向樱桃,最近开发中收集的这篇文章主要介绍iOS 调取iPhone本地相册和开启相机,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

调取本地图片传输到其他的页面,在方法

- (void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info; 中,info为字典值,期中包含你所选择的图片的URL值,可以通过URL值进行传值操作,下面为具体代码:

.h文件

#import <UIKit/UIKit.h>



//签订协议
@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end




.m文件


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIImageView *p_w_picpathView = [[UIImageView alloc] init];

    p_w_picpathView.frame = CGRectMake(80, 50, 200, 120);

    p_w_picpathView.backgroundColor = [UIColor greenColor];

     p_w_picpathView.tag = 1001;

    [self.view addSubview:p_w_picpathView];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(0, 200, 100, 30);

    [button setTitle:@"打开相册" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(openPics) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

 

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button2.frame = CGRectMake(0, 300, 100, 30);

    [button2 setTitle:@"打开相机" forState:UIControlStateNormal];

    [button2 addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];

}



// 打开相机

- (void)openCamera {

    // UIImagePickerControllerCameraDeviceRear 后置摄像头

    // UIImagePickerControllerCameraDeviceFront 前置摄像头

    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];

    if (!isCamera) {

        NSLog(@"没有摄像头");

        return ;

    }

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

    p_w_picpathPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    p_w_picpathPicker.delegate = self;

    // 编辑模式

    p_w_picpathPicker.allowsEditing = YES;  

    [self  presentViewController:p_w_picpathPicker animated:YES completion:^{

    }];    

}

// 打开相册

- (void)openPics {  

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

    p_w_picpathPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    p_w_picpathPicker.delegate = self;

    p_w_picpathPicker.allowsEditing =YES;

    [self  presentViewController:p_w_picpathPicker animated:YES completion:^{

    }];       

}

// 选中照片



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

    NSLog(@"%@", info);

    UIImageView  *p_w_picpathView = (UIImageView *)[self.view viewWithTag:1001];

    // UIImagePickerControllerOriginalImage 原始图片

    // UIImagePickerControllerEditedImage 编辑后图片

    UIImage *p_w_picpath = [info objectForKey:UIImagePickerControllerEditedImage];

    p_w_picpathView.p_w_picpath = p_w_picpath;

    [picker dismissViewControllerAnimated:YES completion:NULL];

    

}

// 取消相册

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

    [picker dismissViewControllerAnimated:YES completion:NULL];

   }

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

转载于:https://blog.51cto.com/10136044/1679368

最后

以上就是外向樱桃为你收集整理的iOS 调取iPhone本地相册和开启相机的全部内容,希望文章能够帮你解决iOS 调取iPhone本地相册和开启相机所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部