IOS应用开发,如何加载图片库中的Image到view中?下面我们来学习一下吧!
操作步骤:
1.创建一个SingleView的项目,然后启动模拟器,点击模拟器上的浏览器,然后将本地的图片拖放上去,然后点击图片长按,点击saveImage到图片库。
2.修改ViewController
ViewController.h:
#import <UIKit/UIKit.h> @interface DXWViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate> @property (retain, nonatomic) IBOutlet UIImageView *imageView; @property (retain, nonatomic) IBOutlet UIButton *Button; @property(retain,nonatomic) UIImage *image; - (IBAction)click:(id)sender; @end
ViewController.m:
#import "DXWViewController.h" @interface DXWViewController () @end @implementation DXWViewController - (void)viewDidLoad { [super viewDidLoad]; } -(void)viewWillAppear:(BOOL)animated { self.imageView.image = self.image; } //加载成功 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { self.Image = [info objectForKey:UIImagePickerControllerEditedImage]; [picker dismissViewControllerAnimated:YES completion:nil]; } //取消 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } - (void)dealloc { [_imageView release]; [_image release]; [_Button release]; [super dealloc]; } //打开图库获取图片 - (IBAction)click:(id)sender { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; } } @end
运行结果:
demo文件:http://download.csdn.net/detail/s10141303/6037575
本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366558,如需转载请自行联系原作者