IOS获取相册中图片以及视频

本文介绍了一个 iOS 应用中实现图片和视频上传的方法。通过 UIAlertController 显示上传选项,利用 UIImagePickerController 控件从相册或摄像头选取媒体文件,并通过代理方法处理用户的选择。

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

          1. 有什么不足希望大家可以一起交流,废话不说了直接代码
            -(void)onUpload
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
                
                UIAlertAction * firstAction = [UIAlertAction actionWithTitle:@"上传图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
                    //设置选取的照片是否可编辑
            //        pickerController.allowsEditing = YES;
                    //设置相册呈现的样式
                    pickerController.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
                    //照片的选取样式还有以下两种
                  
                    //UIImagePickerControllerSourceTypePhotoLibrary,直接全部呈现系统相册
                    //UIImagePickerControllerSourceTypeCamera//调取摄像头
                    // UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
                        
                    //选择完成图片或者点击取消按钮都是通过代理来操作我们所需要的逻辑过程
                    pickerController.delegate = self;
                    //使用模态呈现相册
            
                    [self.navigationController presentViewController:pickerController animated:YES completion:nil];
            
                }];
                
                UIAlertAction * secondAction = [UIAlertAction actionWithTitle:@"上传视频" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
                    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
                    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    pickerController.mediaTypes =@[(NSString*)kUTTypeMovie];
                    pickerController.allowsEditing = NO;
                    pickerController.delegate = self;
                    [self.navigationController presentViewController:pickerController animated:YES completion:nil];
            
                
                }];
                [alert addAction:firstAction];
                [alert addAction:secondAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
            -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
                NSLog(@"%@",info);
                UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
                
                [picker dismissViewControllerAnimated:YES completion:nil];
                NSString *mediaType = info[UIImagePickerControllerMediaType];
                NSLog(@"%@",mediaType);
                if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
                    if (image!=nil) {
                        NSURL *imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
                        NSLog(@"URL:%@",imageURL);
                        NSString *fileName = [imageURL lastPathComponent];
                        NSData * imageData = UIImageJPEGRepresentation(image,0.1);
                        //        NSData * imageData = UIImagePNGRepresentation(image);
                        NSLog(@"压缩到0.1的图片大小:%lu",[imageData length]);
                  
            } }else if([mediaType isEqualToString:(NSString*)kUTTypeMovie]){
            NSLog(
            @"进入视频环节!!!"); NSInteger offset = 1024 * 1024 *5; NSURL *URL = info[UIImagePickerControllerMediaURL]; NSString *URLStr = [NSString stringWithFormat:@"%@",URL]; NSData *file = [NSData dataWithContentsOfURL:URL]; NSLog(@"输出视频的大小:%lu",(unsigned long)[file length]);

转载于:https://www.cnblogs.com/dilz/p/7147441.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值