ipad开发从图片库中加载图片

来源:互联网 发布:三维矩阵旋转 编辑:程序博客网 时间:2024/05/28 16:07

在设计Ipad项目时从图片库中加载图片好处是用户可以通过数据线以及mac电脑上的itunes或预览程序 同步图片信息,这样可以方便图片的管理,应用程序从图片库获取图片,可以通过如下方法:


UIImagePickerController *ipc = [[UIImagePickerControlleralloc] init];

ipc.sourceTypeUIImagePickerControllerSourceTypePhotoLibrary; //图片库的源

ipc.delegate = self;  //代理为self 可见我们应该把用户pick的相关动作处理在当前类中实现,实现代理的协议UIImagePickerControllerDelegate

ipc.allowsImageEditing =NO; //禁止编辑

[selfpresentModalViewController: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 = [NSInvocationinvocationWithMethodSignature: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 = [NSDictionarydictionaryWithObject:image forKey:@"UIImagePickerControllerOriginalImage"];

[selfimagePickerController: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];

}



原创粉丝点击