是否可以在iOS中调整相机预览的大小?

时间:2014-02-03 22:23:10

标签: ios objective-c

我正在尝试让相机预览占据我视图的一部分,而不是整个屏幕。这甚至可能吗?我是否必须使用类似AV Foundation Framework的内容来执行此操作?我是iOS开发的新手,所以任何指导都将非常感谢!谢谢!

2 个答案:

答案 0 :(得分:1)

是的,您需要使用AVFoundation。在过去,有些人(包括我自己)尝试使用UIImagePickerController代替(通过自定义视图替换其view属性),但它在iOS 7中不起作用(或者至少不会像你期望的那样工作)。

答案 1 :(得分:1)

尝试使用此代码将结果图像保持为相同的大小。 对我来说效果很好

第1步:首先为IBOutlet创建UIImageview

第2步:在imagePickerController

中添加自定义方法
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        OriginalImage=info[UIImagePickerControllerOriginalImage];
        image = info[UIImagePickerControllerOriginalImage];

//------Additional method placed here----
            imageview.image = image; // additional method
            [self resizeImage];
//---------
        if (_newMedia)
            UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:finishedSavingWithError:contextInfo:),nil);

    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }

}

步骤3:在相机预览

上调整图像大小的自定义方法
//---- Resize the original image ----------------------
-(void)resizeImage
{
        UIImage *resizeImage = imageview.image;

        float width = 320;
        float height = 320;

        CGSize newSize = CGSizeMake(320,320);
        UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
        CGRect rect = CGRectMake(0, 0, width, height);

        float widthRatio = resizeImage.size.width / width;
        float heightRatio = resizeImage.size.height / height;
        float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;

        width = resizeImage.size.width / divisor;
        height = resizeImage.size.height / divisor;

        rect.size.width  = width;
        rect.size.height = height;

        //indent in case of width or height difference
        float offset = (width - height) / 2;
        if (offset > 0) {
            rect.origin.y = offset;
        }
        else {
            rect.origin.x = -offset;
        }

        [resizeImage drawInRect: rect];

        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

    imageview.image = smallImage;
    imageview.contentMode = UIViewContentModeScaleAspectFit;
}