IOS獲取相冊中圖片以及視頻


          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]);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM