首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIImagePickerController问题?

UIImagePickerController问题?
EN

Stack Overflow用户
提问于 2011-12-02 06:29:44
回答 1查看 1.7K关注 0票数 1

这是我用来呈现UIImagePickerController的代码:

代码语言:javascript
运行
复制
// Show the media browser with our settings, then the browser will call our delegate if needed
- (BOOL) startMediaBrowserFromViewController: (UIViewController*) controller
                               usingDelegate: (id <UIImagePickerControllerDelegate,
                                               UINavigationControllerDelegate>) delegate {

    if (([UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
        || (delegate == nil)
        || (controller == nil))
        return NO;

    UIImagePickerController *mediaUI = [[[UIImagePickerController alloc] init] autorelease];
    mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    // Check for images type
    NSArray * availableTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    BOOL imgsOnlyAvailable = NO;
    // Check if we have only images 
    for (int i = 0; i < [availableTypes count]; i++) {
        // Convert the type
        CFStringRef type = (CFStringRef) [availableTypes objectAtIndex:i];
        if (CFStringCompare ((CFStringRef) type, kUTTypeImage, 0) == kCFCompareEqualTo) {
            // We have images
            imgsOnlyAvailable = YES;
            break;
        }
    }

    // Check if they are available
    if (imgsOnlyAvailable == NO) 
        return NO;

    // Displays only saved pictures from the Camera Roll album.
    mediaUI.mediaTypes = [NSArray arrayWithObject:(id) kUTTypeImage];

    // Hides the controls for moving & scaling pictures, or for
    // trimming movies. To instead show the controls, use YES.
    mediaUI.allowsEditing = NO;

    mediaUI.delegate = delegate;

    [controller presentModalViewController: mediaUI animated: YES];
    return YES;
}

// Picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage;

    // Handle a still image picked from a photo album
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
        == kCFCompareEqualTo) {

        originalImage = (UIImage *) [info objectForKey:
                                     UIImagePickerControllerOriginalImage];    
        // Set image
        self.imgPic.image = originalImage;

        // Now set the button to enabled
        self.btnToText.enabled = YES;
    }

    // Hide picker selector
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}

问题是,每当我尝试单击UIImagePickerController中的图像时,都没有反应(通常它会选择图像,然后关闭控制器)。

有没有什么原因导致事情变得这样糟糕呢?

谢谢!

代码语言:javascript
运行
复制
// Select the image
- (IBAction)btnSelectFileTouchUp:(id)sender {

    // Start the media browser
    if ([self startMediaBrowserFromViewController:self usingDelegate:self] == NO) {
        // Can't open the media browser
        UIAlertView * alrt = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't open the photo media browser in this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alrt show];
        [alrt release];
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-02 06:53:17

查看UIViewController Class Reference中的parentViewController文档。特别是这个小花边新闻。

在iOS 5.0之前,如果视图没有父视图控制器,并且以模态方式呈现,则将返回呈现它的视图控制器。现在已经不是这样了。您可以使用presentingViewController属性获取呈现视图控制器。

在这种情况下,我期望iOS 5上的parentViewController为nil,这就是控制器不会忽略的原因。尝试用presentingViewController替换parentViewController

更新:您必须检查UIViewController上是否存在presentingViewController,以便为低于5.0版的iOS提供行为。

代码语言:javascript
运行
复制
UIViewController *dismissingController = nil;

if ([self respondsToSelector:@selector(presentingViewController)])
    dismissingController = self.presentingViewController;
else
    dismissingController = self.parentViewController;

[dismissingController dismissModalViewControllerAnimated:YES];
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8349228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档