1. 程式人生 > >呼叫手機相簿

呼叫手機相簿

設定info
在這裡插入圖片描述

ap.h裡面

//
//  AppDelegate.h
//  JHH-呼叫相機相簿
//
//  Created by 賈歡 on 2018/5/24.
//  Copyright © 2018年 jiahuan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong) NSPersistentContainer *persistentContainer;

- (void)saveContext;


@end


vc裡面

//
//  ViewController.m
//  JHH-呼叫相機相簿
//
//  Created by 賈歡 on 2018/5/24.
//  Copyright © 2018年 jiahuan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong) UIImageView * imageView;
@property(nonatomic,strong) UIButton * selectBtn;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //頭像
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, self.view.frame.size.width - 200, self.view.frame.size.width - 200)];
    _imageView.backgroundColor = [UIColor lightGrayColor];
    _imageView.layer.cornerRadius =(self.view.frame.size.width - 200)/2;
    _imageView.layer.masksToBounds = YES;
    [self.view addSubview:_imageView];
    
    //按鈕
    _selectBtn = [[UIButton alloc]initWithFrame:CGRectMake(150, 400, self.view.frame.size.width - 300, 50)];
    [_selectBtn setTitle:@"點選選擇照片" forState:UIControlStateNormal];
    [_selectBtn setBackgroundColor:[UIColor redColor]];
    [_selectBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_selectBtn];
   
    
}
-(void)btnClick:(UIButton *)btn{
    // 建立UIImagePickerController例項
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    // 設定代理
    imagePickerController.delegate = self;
    // 是否允許編輯(預設為NO)
    imagePickerController.allowsEditing = YES;
    // 建立一個警告控制器
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選取圖片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    // 設定警告響應事件
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 設定照片來源為相機
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        // 設定進入相機時使用前置或後置攝像頭
        imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        // 展示選取照片控制器
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }];
    UIAlertAction *photosAction = [UIAlertAction actionWithTitle:@"從相簿選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    // 判斷是否支援相機
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        // 新增警告按鈕
        [alert addAction:cameraAction];
    }
    [alert addAction:photosAction];
    [alert addAction:cancelAction];
    // 展示警告控制器
    [self presentViewController:alert animated:YES completion:nil];
}
//設定UIImagePickerController的代理方法:
#pragma mark - UIImagePickerControllerDelegate
// 完成圖片的選取後呼叫的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    // 選取完圖片後跳轉回原控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    /* 此處引數 info 是一個字典,下面是字典中的鍵值 (從相機獲取的圖片和相簿獲取的圖片時,兩者的info值不盡相同)
     * UIImagePickerControllerMediaType; // 媒體型別
     * UIImagePickerControllerOriginalImage; // 原始圖片
     * UIImagePickerControllerEditedImage; // 裁剪後圖片
     * UIImagePickerControllerCropRect; // 圖片裁剪區域(CGRect)
     * UIImagePickerControllerMediaURL; // 媒體的URL
     * UIImagePickerControllerReferenceURL // 原件的URL
     * UIImagePickerControllerMediaMetadata // 當資料來源是相機時,此值才有效
     */
    // 從info中將圖片取出,並載入到imageView當中
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.imageView.image = image;
    // 建立儲存影象時需要傳入的選擇器物件(回撥方法格式固定)
    SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
    // 將影象儲存到相簿(第三個引數需要傳入上面格式的選擇器物件)
    UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, NULL);
}
// 取消選取呼叫的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
//新增儲存圖片完成後的回撥方法:
// 儲存圖片後到相簿後,回撥的相關方法,檢視是否儲存成功
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error == nil){
        NSLog(@"Image was saved successfully.");
    } else {
        NSLog(@"An error happened while saving the image.");
        NSLog(@"Error = %@", error);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end