.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