用iPhone拍照,然后使用它!

时间:2011-04-29 16:54:40

标签: iphone objective-c ios

我有一个应用程序拍摄照片并将其放入图像视图中。简单。 代码如下所示:

- (void)takePhoto:(id)sender
{
    // Lazily allocate image picker controller
    if (!imagePickerController) {
        imagePickerController = [[UIImagePickerController alloc] init];

        // If our device has a camera, we want to take a picture, otherwise, we just pick from
        // photo library
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
        }else
        {
            [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        }

        // image picker needs a delegate so we can respond to its messages
        [imagePickerController setDelegate:self];
    }
    // Place image picker on the screen
    [self presentModalViewController:imagePickerController animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    image = [ImageHelpers imageWithImage:image scaledToSize:CGSizeMake(480, 640)];

    [imageView setImage:image];

    [self dismissModalViewControllerAnimated:YES];
}

当我使用相机胶卷时,一切都很好,但如果我使用实际的相机,则图像视图只是黑色。那是为什么?

在图像视图中使用之前,是否需要将其保存到相机胶卷?

2 个答案:

答案 0 :(得分:6)

确定。我自己找到了解决方案。

我不得不首先解雇模态视图......

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

    [self dismissModalViewControllerAnimated:YES]; //Do this first!!
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    image = [ImageHelpers imageWithImage:image scaledToSize:CGSizeMake(480, 640)];

    [imageView setImage:image];  
}

答案 1 :(得分:1)

为什么你不能只使用那段代码?

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [imageView setImage:image];
    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
}