iOS 从相册加载图片到imageview

来源:互联网 发布:福州seo短期培训 编辑:程序博客网 时间:2024/05/17 20:27

1.新建project,在storyboard中拖入一个button和一个image view

2.在.m文件中给button添加action,

@implementation ViewController


- (IBAction)button:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];

    picker.delegate =self;

    picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

    picker.allowsEditing =YES;

    [selfpresentViewController:picker animated:YEScompletion:NULL];

    

    

    //处理完毕,回到个人信息页面

    [picker dismissViewControllerAnimated:YEScompletion:NULL];

}


@end

3.在.m文件中给image view 添加property。

@interface ViewController ()

@property (weak, nonatomic) IBOutletUIImageView *myimage;

@end



4.在.m文件中添加代码

@implementation ViewController


#pragma mark - image picker delegte


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info


{    

    [picker dismissViewControllerAnimated:YEScompletion:^{}];

    

    UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];

    [self.myimagesetImage:image];  

}


@end

5.运行测试。
0 0