在iphone中使用imagepickercontroller的最佳方法?
我正在使用在iphone中使用imagepickercontroller的最佳方法?,iphone,ios,ipad,Iphone,Ios,Ipad,我正在使用UIImagePickerController从应用程序中的照片库中选择图像。为此,我使用了两种不同的方法。首先,我使用了一个类varioubleUIImagePicker,下面是代码 imagepicker = [[UIImagePickerController alloc]init]; imagepicker.delegate = self; imagepicker.sourceType = UIImagePickerControllerSourceT
UIImagePickerController
从应用程序中的照片库中选择图像。为此,我使用了两种不同的方法。首先,我使用了一个类varioubleUIImagePicker
,下面是代码
imagepicker = [[UIImagePickerController alloc]init];
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:self.imagepicker animated:YES];
上面的代码工作正常。但当我点击按钮时,需要一些时间才能对本例中的动画做出反应。然后我使用了自动释放池方法
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[[UIImagePickerController alloc]init]autorelease];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:picker animated:YES];
}
[pool release];
也很有魅力。两个都显示分析仪没有泄漏。有人能告诉我正确的方法吗。好吧,这里没什么好说的。。。两种方法都有效,两种方法都是正确的,请使用您喜欢的方法
一个小要点:如果您定期演示图像选择器,最好使用第一种方法,并仅在第一次将其分配给实例变量(它不称为“类变量”!),并且在
-dealoc
之前不要释放它,每次用户选择图像时,您都会保存图像选择器的连续分配-取消分配。@angry\u developer无所谓。“两者都能正确地管理内存。但是当我点击按钮时,它需要一些时间才能转到选择器?”愤怒的开发者显然是这样。那么现在呢?@angry_developer AndH2CO3
最好的方法之一就是将imagePickerController
创建为Singleton
。我认为是这样,但对于same@Kamarshad感谢您的支持,但是:对于视图控制器来说,单例并不是最好的模式。见我答案第二部分的建议:)