码迷,mamicode.com
首页 > 其他好文 > 详细

调用系统相册

时间:2015-09-07 12:29:41      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

#import "DetailViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "DBManager.h"
@interface DetailViewController ()
{
}
@end

@implementation DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (_currentModel) {
     //修改界面,为控件赋值
        _nameField.text = _currentModel.userName;
        _ageField.text = _currentModel.age;
        [_headImageBtn setImage:_currentModel.headImage forState:UIControlStateNormal];
    }else{
     //添加界面
    }
    // Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loadSourceWithType:(UIImagePickerControllerSourceType)sourceType{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = sourceType;
    //设置代理
    picker.delegate = self;
    //是否对相册资源进行自动处理
    picker.allowsEditing = YES;
    //在程序中,对于picker习惯于通过模态化的方式呈现出来
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

//根据不同的提示信息,创建警告框
- (void)showAlertWithMessage:(NSString *)message{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}
//调用相册库
- (IBAction)changeImage:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        [self loadSourceWithType:UIImagePickerControllerSourceTypePhotoLibrary];
    }else{
        [self showAlertWithMessage:@"无法调用相册库"];
    }
}
#pragma mark - picker delegate
//点击cancel按钮时,触发此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"cancel!");
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//选中图片资源时(choose),会触发此方法
//info 中带有选中资源的信息
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //先判断资源是否为图片资源(可能是video)
    //获取选中资源的类型
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    //kUTTypeImage 系统预置的图片类型的常量
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        //取到图片
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        [_headImageBtn setImage:image forState:UIControlStateNormal];
    }
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end

调用系统相册

标签:

原文地址:http://www.cnblogs.com/liaods/p/4788464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!