[一句秒懂]从系统中读取图片和拍照功能-上

////////简单的一个从系统中读取图片和拍照功能实现,直接copy过去就可能够使用了//////服务器

iOS有两种的拍照和视频的方式:

方法A:直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。框架

方法B:另外一种是经过AVFoundation.framework框架彻底自定义拍照的界面和选择图片库界面。ide

方法B选择图片下集讲解...........atom


 

文/Smy(简书做者)
原文连接:http://www.jianshu.com/p/dfab715a4987
著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。spa

#import "ViewController.h".net

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height代理

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>code

@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic, strong)UIButton *btn;orm

@end视频

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor redColor];
    btn.frame = CGRectMake((kWidth-100)/2 -25, 200, 100, 100);
    [self.view addSubview:btn];
    self.btn = btn;
    btn.layer.cornerRadius = 50;
    
    
    
    [self.view addSubview:self.imageView];
    
    // 从沙盒中取出图片
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingString:@"aaa"];
    NSLog(@"%@",NSHomeDirectory());
    self.imageView.image = [[UIImage alloc]initWithContentsOfFile:fullPath];
}

- (void)click:(UIButton *)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择照片来源" preferredStyle:0];
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }];
    
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action)
                                   {
                                       //这里能够不写代码
                                   }];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    //用来判断来源 Xcode中的模拟器是没有拍摄功能的,当用模拟器的时候咱们不须要把拍照功能加速
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        
    {
        [alertController addAction:okAction];
        [alertController addAction:cancelAction];
        [alertController addAction:photoAction];
    }
    
    else
    {
        [alertController addAction:okAction];
        [alertController addAction:cancelAction];
    }
    
}

 

#pragma mark - 代理


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    //选取裁剪后的图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    /* 此处info 有六个值
     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
     * UIImagePickerControllerMediaURL;       // an NSURL
     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
     */
    
     self.imageView.image = image;
    
    // 保存图片到沙盒目录中
    [self saveImage:image withName:@"aaa"];
    
    // 上传图片到服务器中。。。待续本身去作吧
    
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    NSLog(@"取消选择照片");
    [picker dismissViewControllerAnimated:YES completion:^{}];
}


#pragma mark - 保存图片到沙盒中

- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName {
    
    // 高保真压缩图片,但图片质量基本保持不变,第二个参数即图片质量参数
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ] stringByAppendingString:imageName];
    
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

 

#pragma mark - get

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kWidth-100)/2 -25, 50, 100, 100)];
        _imageView.backgroundColor = [UIColor whiteColor];
        _imageView.layer.cornerRadius = 50;
        _imageView.clipsToBounds = YES;
    }
    return _imageView;
}

@end

相关文章
相关标签/搜索