iOS获取相册图片视频

本文通过实例代码展示了如何在iOS应用中获取相册图片和视频,包括从相册选取和使用相机拍照的功能实现,详细解释了相关代码逻辑。

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

借鉴好多,写了个demo

Demo:直接上代码

#import "ViewController.h"


@interface ViewController ()<UIImagePickerControllerDelegate>

{

    UIButton *_button;

}

@property (nonatomic, copy) NSString *imagePath;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    /**

     *  获取Documents文件夹路径

     *

     *

     */

//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

    self.view.backgroundColor = [UIColor redColor];

    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    _button.frame = CGRectMake(100, 100, 100, 100);

    [_button setTitle:@"添加照片" forState:UIControlStateNormal];

    [_button addTarget:self action:@selector(addPic) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_button];

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = path[0];

    //指定新建文件夹路径

    NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"];

    //创建ImageFile文件夹

    [[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil  ];

    //保存图片路径

    self.imagePath = [imageDocPath stringByAppendingPathComponent:@"image.png"];

}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:YES];

    //根据图片路径载入图片

    UIImage *image = [UIImage imageWithContentsOfFile:self.imagePath];

    if (image == nil) {

        [_button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

        

    }

    else{

        [_button setBackgroundImage:image forState:UIControlStateNormal];

    }

}

- (void)addPic

{

    NSString *cancelButtonTitle = NSLocalizedString(@"从相册选取", nil);

    NSString *otherButtonTitle = NSLocalizedString(@"拍照", nil);

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

       [self LocalPhoto];

    }];

    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

         [self takePhoto];

    }];

    [alertController addAction:cancelAction];

    [alertController addAction:otherAction];

    [self presentViewController:alertController animated:YES completion:nil];

}

- (void)LocalPhoto

{

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    //从资源类型为图片库

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    picker.delegate = self;

    //设置选择后图片可以被编辑

    picker.allowsEditing = YES;

    [self presentViewController:picker animated:YES completion:^{

        

    }];

}



- (void)takePhoto

{

    //资源类型为照相机

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

    //判断是否有相机

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIImagePickerController *picker = [[UIImagePickerController alloc]init];

        picker.delegate = self;

        picker.sourceType = sourceType;

        [self presentViewController:picker animated:YES completion:^{

            

        }];

    }

    else

    {

        NSLog(@"该设备无摄像头");

    }

}

#pragma mark - 

#pragma mark Delegate method UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo


{

    if (image != nil) {

        //图片显示在界面上

        [_button setBackgroundImage:image forState:UIControlStateNormal];

        

        //保存文件到沙盒

        //将图片转成NSData类型的数据来保存文件

        NSData *data;

        //判断图片是不是png格式的文件

        if (UIImagePNGRepresentation(image)) {

            //返回为png图像

            data = UIImagePNGRepresentation(image);

        }

        else

        {

            //返回为JPEG图像

            data = UIImageJPEGRepresentation(image, 1.0);

        }

        //保存

        [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];

    }

    //关闭相册界面

    [picker dismissViewControllerAnimated:YES completion:^{

        NSLog(@"sb");//关闭相册回到界面做的事情

    }];

}

@end




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值