2011年5月22日 星期日

UIImagePickerController

可用來選取照片(從photo library中)或重新拍照(使用camera)...在使用的時候需要先產生UIImagePickerController  實作, 為其指定委託(delegate), 並選擇圖像來源類型, 設定圖像是否可編輯,  最後將其啓動, 顯示其檢視



建立UIImagePickerController

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


設定來源
// 透過相機取的新的照片


imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera

// 圖片位置為iPhone photo library
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary

// 使用照片保存相簿為來源
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum



一般指定來源之前 ,  要檢查一下 , 以確保指定的圖片來源可用, 可使用 isSourceTypeAvailable


if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary
{
<#statements#>
}



指定委託, 委託必須實作UIImagePickerControllerDelegate協議 , 來對使用者在圖像選取器中的動作(例如點擊 "cancel"按鈕)

imagePicker.delegate = self;


設定為可編輯

imagePicker.allowsEditing = YES;


啓動

 [self presentModalViewController:imagePicker animated:YES];


UIImagePickerControllerDelegate協議

//Tells the delegate that the user picked a still image or movie.
 (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
}
//Tells the delegate that the user cancelled the pick operation.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
}

//Tells the delegate that the user picked an image. (Deprecated in iOS 3.0. Use imagePickerController:didFinishPickingMediaWithInfo: instead.)
- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingImage:(UIImage *)image 
  editingInfo:(NSDictionary *)editingInfo
{
}


收回UIImagePickerController

[imagePicker dismissModalViewControllerAnimated:YES];

沒有留言:

張貼留言