ios中UIImagePickerController 圖片選取器的使用

來源:互聯網
上載者:User

1。 定義 類變數:
UIImagePickerController* picker_library_;
2。實現 UIImagePickerControllerDelegate 這個delegate,還需要UINavigationControllerDelegate 這個代理
3。 以模態的方式,顯示 圖片選取器

picker_library_ = [[UIImagePickerController alloc] init];          picker_library_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;          picker_library_.allowsEditing = YES;          picker_camera_.allowsImageEditing=YES;          picker_library_.delegate = self;          [self presentModalViewController: picker_library_                                  animated: YES];  

其中,sourceType 指定了 幾種 圖片的來源:
UIImagePickerControllerSourceTypePhotoLibrary:表示顯示所有的照片
UIImagePickerControllerSourceTypeCamera:表示從網路攝影機選取照片
UIImagePickerControllerSourceTypeSavedPhotosAlbum:表示僅僅從相簿中選取照片。
allowEditing和allowsImageEditing 設定為YES,表示 允許使用者編輯圖片,否則,不允許使用者編輯。
4。 當使用者選擇一個圖片以後,有可能調用兩種不同的函數,根據版本的不同。所以,如果要同時支援高版本和低版本的相容性,那麼就
要處理兩種函數。
//3.x 使用者選中圖片後的回調
- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
//2.x 使用者選中圖片之後的回調
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
兩種函數的處理代碼如下:

//3.x  使用者選中圖片後的回調      - (void)imagePickerController: (UIImagePickerController *)picker       didFinishPickingMediaWithInfo: (NSDictionary *)info      {          if (picker == picker_camera_)           {              //如果是 來自照相機的image,那麼先儲存              UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];              UIImageWriteToSavedPhotosAlbum(original_image, self,                                              @selector(image:didFinishSavingWithError:contextInfo:),                                              nil);          }                    //獲得編輯過的圖片          UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"];                              [self dismissModalViewControllerAnimated:YES];          [picker release];      }  

//2.x  使用者選中圖片之後的回調      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo      {          NSMutableDictionary * dict= [NSMutableDictionary dictionaryWithDictionary:editingInfo];                    [dict setObject:image forKey:@"UIImagePickerControllerEditedImage"];                    //直接調用3.x的處理函數          [self imagePickerController:picker didFinishPickingMediaWithInfo:dict];      }  

5。 使用者取消選擇

// 使用者選擇取消      - (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker      {          [self dismissModalViewControllerAnimated:YES];          [picker release];      }  

6。示範樣本

- (void) pickImage: (id) sender{     UIImagePickerController *ipc = [[UIImagePickerController alloc] init];     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){          ipc.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;          ipc.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:ipc.sourceType];        }      ipc.delegate = self;     ipc.allowsImageEditing = NO;     [self presentModalViewController:ipc animated:YES]; }- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{     NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];    if ([mediaType isEqualToString:@"public.image"]){         // UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];         UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];         NSLog(@"found an image");         [UIImageJPEGRepresentation(image, 1.0f) writeToFile:[self findUniqueSavePath] atomically:YES];         SETIMAGE(image);         CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory()     stringByAppendingString:@"/Documents"]]);    }    else if ([mediaType isEqualToString:@"public.movie"]){        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];        NSLog(@"found a video");        NSData *webData = [NSData dataWithContentsOfURL:videoURL];        //NSData *video = [[NSString alloc] initWithContentsOfURL:videoURL];        [webData writeToFile:[self findUniqueMoviePath] atomically:YES];        CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);        // NSLog(videoURL);    }    [picker dismissModalViewControllerAnimated:YES];}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.