das*_*ist 15 iphone uiimagepickercontroller ios ios7
我对使用UIImagePickerController
整个屏幕的摄像机控件的自定义叠加感兴趣,非常类似于切换到视频功能时默认的摄像头应用程序所做的操作.
关于这个主题,我在这里引用了几个问题,尤其是这个问题,以及关于如何为UIImagePickerController使用自定义叠加层的Apple示例,但是它们都没有能够产生成功的全屏结果IOS 7.
到目前为止,这是相机UI与自定义叠加层(没有任何按钮)的样子:
请注意,屏幕底部有一个黑条.我注意到如果我将其更改为1,我可以删除黑条cameraAspectRatio
,但是这会使相机变焦,因为它具有非常放大的视图.有没有办法让全屏没有太多扭曲变焦(我明白一点点是必要的),还删除黑条?
这是我配置自定义叠加层的代码:
{
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.frame;
self.imagePickerController.cameraOverlayView = self.overlayView;
self.overlayView = nil;
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
float cameraAspectRatio = 4.0 / 3.0; //! Note: 4.0 and 4.0 works
float imageWidth = floorf(screenSize.width * cameraAspectRatio);
float scale = ceilf((screenSize.height / imageWidth) * 10.0) / 10.0;
self.imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
}
Run Code Online (Sandbox Code Playgroud)
更新:
我注意到的另一个问题是,相机预览中显示的图片总是较小,并且细节少于方法时保存的图片[self.imagePickerController takePicture]
.为了显示:
这是键盘在相机预览中看起来像下面的黑色条(抱歉它有点不稳定):
但请注意,预览面板中的实际捕获图像具有更多详细信息,如此处所示,尤其是朝向顶部,以及左侧和右侧.
我的问题是,如何设置我的相机预览,以便用户在预览中看到的是它将捕获的图像,然后可以显示给他们?谢谢!
更新2
以下是viewDidLoad
设置相机控件的完整代码.
- (void)viewDidLoad
{
[super viewDidLoad];
//Appearance configuration
self.navigationItem.hidesBackButton = YES;
//UIImagePickerController initialization and setup
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
self.imagePickerController.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; //if there is a camera avaliable
} else {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//otherwise go to the folder
}
self.imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
if (self.imagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera)
{
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.frame;
self.imagePickerController.cameraOverlayView = self.overlayView;
self.overlayView = nil;
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
int heightOffset = 0;
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
heightOffset = 120; //whole screen :)
}
float cameraAspectRatio = 4.0 / 3.0; //! Note: 4.0 and 4.0 works
float imageWidth = floorf(screenSize.width * cameraAspectRatio);
float scale = ceilf(((screenSize.height + heightOffset) / imageWidth) * 10.0) / 10.0;
self.imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
}
}
Run Code Online (Sandbox Code Playgroud)
在viewWillAppear
,我调用图像选择器视图:
BOOL modalPresent = (BOOL)(self.presentedViewController);
//Present the Camera UIImagePicker if no image is taken
if (!appDelegate.imageStorageDictionary[@"picture1"]){
if (modalPresent == NO){ //checks if the UIImagePickerController is modally active
[self presentViewController:self.imagePickerController animated:NO completion:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
das*_*ist 26
经过多次尝试,这对我有用,非常感谢其他人的建议.以下事实非常有助于了解并牢记:
相机的点分辨率为426*320.为了将相机预览的高度拉伸到手机的屏幕高度568,使用时需要乘以系数1.3333 CGAffineTransformScale
.
请注意,下面的内容是基于iPhone 5的分辨率屏幕分辨率使用各种数字进行硬编码的.它们可以通过使用诸如screen.height,screen.width和其他变量之类的对象来改进,以使其适用于iPhone 4/4s尺寸.
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.frame;
self.imagePickerController.cameraOverlayView = self.overlayView;
self.overlayView = nil;
//For iphone 5+
//Camera is 426 * 320. Screen height is 568. Multiply by 1.333 in 5 inch to fill vertical
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
self.imagePickerController.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
self.imagePickerController.cameraViewTransform = scale;
Run Code Online (Sandbox Code Playgroud)
在斯威夫特
var picker: UIImagePickerController = UIImagePickerController();
picker.sourceType = UIImagePickerControllerSourceType.Camera;
picker.showsCameraControls = false;
var screenBounds: CGSize = UIScreen.mainScreen().bounds.size;
var scale = screenBounds.height / screenBounds.width;
picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, scale, scale);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33183 次 |
最近记录: |