从相机拍摄照片并在UIImageView中显示 [英] Taking a picture from the camera and show it in a UIImageView

查看:138
本文介绍了从相机拍摄照片并在UIImageView中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些视图,包括一些字段(名称,价格,类别)和分段控件,以及此按钮可以拍照。

如果我在模拟器上尝试这个(没有摄像头)它可以正常工作:我可以从相机胶卷中选择图像,编辑它并返回视图,它将显示所有字段及其内容。

但是在我的iphone上,当我在编辑后选择图像并返回视图时,UIImageView的所有字段都是空的。我也尝试保存字段的内容在变量中并将它们放回viewWillApper方法中,但应用程序崩溃了。

开始考虑下面可能有错误的方法

I have a view with some fields (name, price, category) and a segmented control, plus this button to take picture.
If I try this on the simulator (no camera) it works properly: I can select the image from the camera roll, edit it and go back to the view, which will show all the fields with their contents .
But on my iphone, when I select the image after the editing and go back to the view, all the fields are empty exept for the UIImageView.
I also tried to save the content of the fields in variables and put them back in the "viewWillApper" method, but the app crashes.
Start to thinking that maybe there is something wrong methods below

编辑

我找到了解决方案此处。我为UIImage类定义了一个新方法。 (点击链接获取更多信息)。
然后,我在UIImageView的框架上工作,以适应横向或纵向的新维度。

I found the solution here. I defined a new method to the UIImage class. (follow the link for more information).
Then I worked on the frame of the UIImageView to adapt itself to the new dimension, in landscape or portrait.

-(IBAction)takePhoto:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imgPicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    } else { 
        imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    [self presentModalViewController:self.imgPicker animated:YES];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];

    NSDate *date = [NSDate date];
    NSString *photoName = [dateFormatter stringFromDate:date];    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUs    erDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", photoName]];

    UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];

    // ---------RESIZE CODE--------- //
    if (picture.size.width == 1936) {
        picture = [picture scaleToSize:CGSizeMake(480.0f, 720.0f)];
    } else {
        picture = [picture scaleToSize:CGSizeMake(720.0f, 480.0f)];
    }
    // --------END RESIZE CODE-------- //

    photoPreview.image =  picture;

    // ---------FRAME CODE--------- //
    photoPreview.contentMode = UIViewContentModeScaleAspectFit;
    CGRect frame = photoPreview.frame;
    if (picture.size.width == 480) {
        frame.size.width = 111.3;
        frame.size.height =167;
    } else {
        frame.size.width = 167;
        frame.size.height =111.3;
    }
    photoPreview.frame = frame;
    // --------END FRAME CODE-------- //


    NSData *webData = UIImagePNGRepresentation(picture);
    CGImageRelease([picture CGImage]);
    [webData writeToFile:imagePath atomically:YES];
    imgPicker = nil;
}

现在我有了一个新问题!如果我在风景中拍照,并试图拍摄另一张照片,那么应用就会崩溃。我是否需要发布一些东西?

Now I have a new issue! If I take a picture in landscape, and try to take another one in portrait, the app crashs. Do I have to release something?

推荐答案

我遇到了同样的问题,使用相机时没有编辑过的图像,你必须使用原始图像:

I had the same issue, there is no edited image when using the camera, you must use the original image :

originalimage = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];

if ([editingInfo objectForKey:UIImagePickerControllerMediaMetadata]) {
    // test to chek that the camera was used
    // especially I fund out htat you then have to rotate the photo
    ...

如果在使用相册时裁剪,则需要重新裁剪当然:

If it was cropped when usign the album you have to re-crop it of course :

if ([editingInfo objectForKey:UIImagePickerControllerCropRect] != nil) {
    CGRect cropRect = [[editingInfo objectForKey:UIImagePickerControllerCropRect] CGRectValue];
    CGImageRef imageRef = CGImageCreateWithImageInRect([originalimage CGImage], cropRect);
    chosenimage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
} else {
    chosenimage = originalimage;
}

相机模式也有裁剪信息,你需要检查你希望它表现得好。

The croprect info is also present for the camera mode, you need to check how you want it to behave.

这篇关于从相机拍摄照片并在UIImageView中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆