[一句秒懂]從系統中讀取圖片和拍照功能-上

////////簡單的一個從系統中讀取圖片和拍照功能實現,直接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

相關文章
相關標籤/搜索