首页 > 其他 > 详细

ios从系统相册中读取图片

时间:2014-02-28 12:41:38      阅读:407      评论:0      收藏:0      [点我收藏+]

.h
#import <UIKit/UIKit.h>
@interface HViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (strong, nonatomic) UIImageView *imageView;
@end


.m
#import "HViewController.h"
@interface HViewController ()
@end
@implementation HViewController
- (void)dealloc
{
    [_imageView release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *pBtn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    pBtn1.frame = CGRectMake(0, 30, 50, 40);
    [pBtn1 setTitle:@"取图片" forState:UIControlStateNormal];
    [pBtn1 addTarget:self action:@selector(pBtn1:)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pBtn1];
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 90, 240, 300)];
    [self.view addSubview:_imageView];
}
- (void)pBtn1 :(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
        [picker release];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"访问图片库错误"
                              message:@""
                              delegate:nil
                              cancelButtonTitle:@"OK!"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
//再调用以下委托:
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    _imageView.image = image; //imageView为自己定义的UIImageView
    [picker dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


ios从系统相册中读取图片,布布扣,bubuko.com

ios从系统相册中读取图片

原文:http://blog.csdn.net/huang_zhidong/article/details/20036033

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!