在設計Ipad項目時從圖片庫中加載圖片好處是用戶可以通過數據線以及mac電腦上的itunes或預覽程序 同步圖片信息,這樣可以方便圖片的管理,應用程序從圖片庫獲取圖片,可以通過如下方法:
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //圖片庫的源
ipc.delegate = self; //代理爲self 可見我們應該把用戶pick的相關動作處理在當前類中實現,實現代理的協議UIImagePickerControllerDelegate
ipc.allowsImageEditing = NO; //禁止編輯
[self presentModalViewController:ipc animated:YES] ; //彈出模式對話框
之後就是對用戶選取的處理了
@interface TestBedViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end
@implementation TestBedViewController
// 3.0-3.1 提供
- (void) setAllowsEditing:(BOOL)doesAllow forPicker:(UIImagePickerController *) ipc
{
SEL allowsSelector;
if ([ipc respondsToSelector:@selector(setAllowsEditing:)]) allowsSelector = @selector(setAllowsEditing:);
NSMethodSignature *ms = [ipc methodSignatureForSelector:allowsSelector];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:ms];
[inv setTarget:ipc];
[inv setSelector:allowsSelector];
[inv setArgument:&doesAllow atIndex:2];
[inv invoke];
}
// 2.x 提供 當用戶選取完成一副圖片庫中的圖片保存到字典中
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSDictionary *dict = [NSDictionary dictionaryWithObject:imageforKey:@"UIImagePickerControllerOriginalImage"];
[self imagePickerController:picker didFinishPickingMediaWithInfo:dict];
}
// 如果用戶取消
- (void) imagePickerControllerDidCancel:
(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
//從字典獲取圖片並顯示到當前窗口,同時關閉模態窗口
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
SETIMAGE([info objectForKey:@"UIImagePickerControllerOriginalImage"]);
[self dismissModalViewControllerAnimated:YES];
[picker release];
}