iPhone-从相机拍摄照片并将其显示在UIImageView中

我有一个包含一些字段(名称,价格,类别)和分段控件的视图,以及此按钮用来拍照.
如果我在模拟器(没有相机)上尝试此操作,它将正常工作:我可以从相机胶卷中选择图像,对其进行编辑,然后返回视图,该视图将显示所有字段及其内容.
但是在我的iPhone上,当我在编辑后选择图像并返回视图时,UIImageView的所有字段均为空.我还尝试将字段的内容保存在变量中,然后将其放回“ viewWillApper ”方法,但应用程序崩溃.
开始认为以下可能存在错误的方法

编辑

我找到了解决方案here.我为UIImage类定义了一个新方法. (点击链接以获取更多信息).然后,我在UIImageView的框架上进行了工作,以使其适应横向或纵向的新尺寸.

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

现在我有一个新问题!如果我横向拍摄照片,然后尝试纵向拍摄另一张照片,则应用程序崩溃.我需要释放一些东西吗?

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

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

相机模式中也存在裁剪信息,您需要检查其行为方式.

转载注明原文:iPhone-从相机拍摄照片并将其显示在UIImageView中 - 代码日志