【问题标题】:Taking a picture from the camera and show it in a UIImageView从相机拍照并在 UIImageView 中显示
【发布时间】:2011-10-19 11:18:26
【问题描述】:

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

现在我有一个新问题!如果我以横向拍摄一张照片,然后尝试以纵向拍摄另一张,则应用程序会崩溃。我必须发布一些东西吗?

【问题讨论】:

    标签: iphone objective-c ios uiimagepickercontroller viewwillappear


    【解决方案1】:

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

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

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

    【讨论】:

    • 非常感谢。现在我有了图像。但是,真的,我不能使用 3.1 MB 的照片……有没有办法使用编辑过的?还是压缩原件?
    • 我愿意:UIImage*compressedimage = [UIImage imageWithData:UIImageJPEGRepresentation(chosenimage, 0.0)];压缩图像。 0.0 是要调整的压缩比,这导致每张图像大约 100 kb
    • 还有0.0每张图片大于2MB。
      没有办法使用控制器的“editedImage”或“cropRect”吗?
    • 我不知道editedImage,你可以使用cropRect,但要小心它有值,即使你没有自己裁剪。至于大小,我重新阅读了我的代码并注意到我两次应用了 UIImageJPEG... 方法,值得一试
    • 我不知道editedImage,你可以使用cropRect,但要小心它有值,即使你没有自己裁剪。至于大小,我重新阅读了我的代码并注意到我两次应用了 UIImageJPEG... 方法,值得一试
    【解决方案2】:

    要裁剪图像,我认为这可能会对您有所帮助

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

    【讨论】:

    • 以上方法可以缩放图像,也可以降低分辨率,所以尺寸也可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    相关资源
    最近更新 更多