zoukankan      html  css  js  c++  java
  • 186使用系统内的相册

    PS:对于 Camera 选项,会调用摄像头,需要真机才能测试。

    效果如下:

    ViewController.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    4 @property (strong, nonatomic) UIImageView *imgVCustom;
    5 
    6 @end

    ViewController.m

     1 #import "ViewController.h"
     2 
     3 @interface ViewController ()
     4 - (void)layoutUI;
     5 - (void)showActionSheet:(UIBarButtonItem *)sender;
     6 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad {
    12     [super viewDidLoad];
    13     
    14     [self layoutUI];
    15 }
    16 
    17 - (void)didReceiveMemoryWarning {
    18     [super didReceiveMemoryWarning];
    19     // Dispose of any resources that can be recreated.
    20 }
    21 
    22 - (void)viewWillAppear:(BOOL)animated {
    23     [super viewWillAppear:animated];
    24     [self.navigationController setNavigationBarHidden:NO animated:animated];
    25     [self.navigationController setToolbarHidden:NO animated:animated];
    26 }
    27 
    28 - (void)layoutUI {
    29     self.navigationItem.title = @"使用系统内的相册";
    30     self.view.backgroundColor = [UIColor whiteColor];
    31     UIBarButtonItem *barBtnChooseImage = [[UIBarButtonItem alloc]
    32                                           initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
    33                                           target:self
    34                                           action:@selector(showActionSheet:)];
    35     self.toolbarItems = @[barBtnChooseImage];
    36     
    37     _imgVCustom = [[UIImageView alloc] initWithFrame:self.view.bounds];
    38     _imgVCustom.contentMode = UIViewContentModeScaleAspectFit;
    39     _imgVCustom.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    40     _imgVCustom.image = [UIImage imageNamed:@"1"];
    41     [self.view addSubview:_imgVCustom];
    42 }
    43 
    44 - (void)showActionSheet:(UIBarButtonItem *)sender {
    45     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择相册"
    46                                                              delegate:self
    47                                                     cancelButtonTitle:@"取消"
    48                                                destructiveButtonTitle:nil
    49                                                     otherButtonTitles:@"PhotoLibrary", @"Camera", @"SavedPhotosAlbum", nil];
    50     [actionSheet showFromToolbar:self.navigationController.toolbar];
    51 }
    52 
    53 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    54     if (error) {
    55         NSLog(@"%@", [error localizedDescription]);
    56     }
    57 }
    58 
    59 #pragma mark - UIActionSheetDelegate
    60 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    61     if (buttonIndex != actionSheet.cancelButtonIndex) {
    62         UIImagePickerControllerSourceType sourceType = buttonIndex;
    63         if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
    64             UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    65             imagePickerController.delegate = self;
    66             imagePickerController.sourceType = sourceType;
    67             imagePickerController.allowsEditing = YES;
    68             [self presentViewController:imagePickerController
    69                                animated:YES
    70                              completion:nil];
    71         }
    72     }
    73 }
    74 
    75 #pragma mark - UIImagePickerControllerDelegate
    76 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    77     UIImage *imgChoice = [info objectForKey:UIImagePickerControllerEditedImage];
    78     if (!imgChoice) {
    79         imgChoice = [info objectForKey:UIImagePickerControllerOriginalImage];
    80     }
    81     _imgVCustom.image = imgChoice;
    82     
    83     //把图片保存到相册
    84 //    UIImageWriteToSavedPhotosAlbum(imgChoice,
    85 //                                   self,
    86 //                                   @selector(image:didFinishSavingWithError:contextInfo:),
    87 //                                   NULL);
    88     
    89     [self imagePickerControllerDidCancel:picker];
    90 }
    91 
    92 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    93     [self dismissViewControllerAnimated:YES completion:nil];
    94 }
    95 
    96 @end

    AppDelegate.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    4 @property (strong, nonatomic) UIWindow *window;
    5 @property (strong, nonatomic) UINavigationController *navigationController;
    6 
    7 @end

    AppDelegate.m

     1 #import "AppDelegate.h"
     2 #import "ViewController.h"
     3 
     4 @interface AppDelegate ()
     5 @end
     6 
     7 @implementation AppDelegate
     8 
     9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    11     ViewController *viewController = [[ViewController alloc] init];
    12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    13     _window.rootViewController = _navigationController;
    14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
    15     [_window makeKeyAndVisible];
    16     return YES;
    17 }
    18 
    19 - (void)applicationWillResignActive:(UIApplication *)application {
    20 }
    21 
    22 - (void)applicationDidEnterBackground:(UIApplication *)application {
    23 }
    24 
    25 - (void)applicationWillEnterForeground:(UIApplication *)application {
    26 }
    27 
    28 - (void)applicationDidBecomeActive:(UIApplication *)application {
    29 }
    30 
    31 - (void)applicationWillTerminate:(UIApplication *)application {
    32 }
    33 
    34 @end
  • 相关阅读:
    php的session和cookie
    CRUD
    hibernate关系映射
    hibernate hql
    String和StringBuffer的区别
    策略模式Strategy
    项目结构
    final关键字
    项目中的建议
    struts学习记录
  • 原文地址:https://www.cnblogs.com/huangjianwu/p/4584107.html
Copyright © 2011-2022 走看看