从相机拍摄照片并在UIImageView中显示

时间:2011-10-19 11:18:27

标签: iphone objective-c ios uiimagepickercontroller viewwillappear

我有一些视图,包括一些字段(名称,价格,类别)和分段控件,以及此按钮可以拍照。
如果我在模拟器(没有摄像头)上尝试这个,它可以正常工作:我可以从相机胶卷中选择图像,编辑它并返回到视图,这将显示包含其内容的所有字段。
但是在我的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;
}

现在我有了一个新问题!如果我在风景中拍照,并试图拍摄另一张照片,应用程序会崩溃。我必须发布一些东西吗?

2 个答案:

答案 0 :(得分:3)

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

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

相机模式也存在裁剪信息,您需要检查它的行为方式。

答案 1 :(得分:0)

裁剪图片我觉得这可能对你有帮助

UIImage *croppedImage = [self imageByCropping:photo.image toRect:tempview.frame];

CGSize size = CGSizeMake(croppedImage.size.height, croppedImage.size.width);
UIGraphicsBeginImageContext(size);

CGPoint pointImg1 = CGPointMake(0,0);
[croppedImage drawAtPoint:pointImg1 ];

[[UIImage imageNamed:appDelegete.strImage] drawInRect:CGRectMake(0,532, 150,80) ];

UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
croppedImage = result;

UIImageView *mainImageView = [[UIImageView alloc] initWithImage:croppedImage];

CGRect clippedRect = CGRectMake(0, 0, croppedImage.size.width,  croppedImage.size.height);

CGFloat scaleFactor = 0.5;

UIGraphicsBeginImageContext(CGSizeMake(croppedImage.size.width * scaleFactor, croppedImage.size.height * scaleFactor));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextClipToRect(currentContext, clippedRect);   

//this will automatically scale any CGImage down/up to the required thumbnail side (length) when the CGImage gets drawn into the context on the next line of code
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);

[mainImageView.layer renderInContext:currentContext];

appDelegete.appphoto = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
相关问题