iOS中调用系统相册以及使用系统相册的照片

本文介绍了一个iOS应用中实现从相册选择图片并进行编辑的功能。通过使用UIImagePickerController,可以轻松地让用户从设备的相册中挑选图片,并提供简单的编辑选项。代码展示了如何设置图片选择控制器以及如何处理用户的选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "ViewController.h"

@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic,strong)UIImageView *headImageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    _headImageView.backgroundColor = [UIColor yellowColor];
    _headImageView.layer.cornerRadius = 50;
    _headImageView.layer.masksToBounds = YES;
    _headImageView.tag = 101;
    [self.view addSubview:_headImageView];
    
    _headImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_headImageView addGestureRecognizer:tap];
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)tapAction:(UIImageView*)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:^{
        NSLog(@"打开相册");
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{
        NSLog(@"取消");
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    _headImageView.image = info[UIImagePickerControllerEditedImage];
    NSLog(@"%@",info);
    [picker dismissViewControllerAnimated:YES completion:^{
        NSLog(@"选照片");
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

转载于:https://www.cnblogs.com/cityingma/p/4883401.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值