【问题标题】:Best way to use imagepickercontroller in iphone?在 iphone 中使用 imagepickercontroller 的最佳方法?
【发布时间】:2012-10-19 12:57:19
【问题描述】:

我正在使用UIImagePickerController 从我的应用程序的 PhotoLibrary 中选择图像。为此,我使用了两种不同的方法。起初我使用了一个类变量UIImagePicker 和下面的代码。

     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];

还有魅力。他们都显示分析仪没有泄漏。谁能指出正确的方法。

【问题讨论】:

    标签: iphone ios ipad


    【解决方案1】:

    好吧,这里没什么好说的......两种方法都有效,两种方法都是正确的,使用你喜欢的任何一个。

    一个小问题:如果你经常展示图像选择器,你最好使用第一种方法,并将它分配给一个实例变量(它不称为“类变量”!)仅用于第一种时间, 并且在 - dealloc 之前不要释放它 - 这样,每次用户选择图像时,您都会保存图像选择器的连续分配 - 释放。

    【讨论】:

    • @angry_developer 没关系。两者都正确管理内存。
    • ut 当我点击按钮时需要一些时间才能进入选择器?
    • @angry_developer 显然是这样。现在呢?
    • @angry_developer 和 H2CO3 最好的方法之一可能是如果您将 imagePickerController 创建为 Singleton 。我认为是这样,但仍然 +1 相同
    • @Kamarshad 感谢您的支持,但仍然:单例不是视图控制器的最佳模式。请参阅我的答案第二部分中的建议:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    相关资源
    最近更新 更多