iphone - 用 iPhone 拍照,然后使用它!

标签 iphone objective-c ios

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

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

当我使用相机胶卷时一切正常,但如果我使用实际的相机, ImageView 只是黑色。这是为什么?

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

最佳答案

好的。自己找到了解决方案。

我不得不先关闭模态视图...

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

关于iphone - 用 iPhone 拍照,然后使用它!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5834957/

相关文章:

php - iOS - Swift 中的密码加密

ios - 如何使单元格适合 Collection View

iphone - MBProgresshud 将其设置为横向

iphone - 深拷贝和浅拷贝

iphone - iPhone相机闪光灯问题

objective-c - 在 Objective-C 中读取 MS Access .MDB 数据库

ios - Tabbar Controller 在 presentviewcontroller ios 时显示黑屏

iphone - 连接 MSSQL 数据库以在 Objective C 中安全登录

objective-c - 何时使用(或不使用)委托(delegate)

iphone - 如何在 iOS 中制作其他语言的键盘