如何使用自定义uiimagepickerrControllerCropRect-iphone,ios,camera

iOS摄像头和相册-UIImagePickerController-浅析
在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频。并且从相册中选取我们需要的图片或者视频。
关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制。
关于UIImagePickerController的相关知识,
iOS的一些设备上都安装了摄像头。现在绝大多数都有了。
在编程中,我们是用相应的东西来进行照相,录像等功能。
一、UIImagePickerController类
UIImagePickerController这个类可以为大家提供照相的功能,以及图片,视频浏览的功能。&
二、检查硬件是否安装有摄像头或者允许操作相册
这些公共的方法,我们也许会用到,我就贴了!So
easy!!!
#pragma mark -&摄像头和相册相关的公共类
//&判断设备是否有摄像头
isCameraAvailable{
&&return&[UIImagePickerController&isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
//&前面的摄像头是否可用
isFrontCameraAvailable{
&&return&[UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
//&后面的摄像头是否可用
isRearCameraAvailable{
&&return&[UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
//&判断是否支持某种多媒体类型:拍照,视频
cameraSupportsMedia:(NSString&*)paramMediaType
sourceType:(UIImagePickerControllerSourceType)paramSourceType{
& &&__block&BOOL&result
& &&if&([paramMediaType&length]
&&NSLog(@"Media type is empty.");
&&return&NO;
& &&NSArray&*availableMediaTypes
=[UIImagePickerControlleravailableMediaTypesForSourceType:paramSourceType];
[availableMediaTypes&enumerateObjectsUsingBlock:^(id&obj,&NSUInteger&idx,&BOOL*stop)
&&NSString&*mediaType
= (NSString&*)
&&if&([mediaTypeisEqualToString:paramMediaType]){
& & & result
*stop=&YES;
& &&return&
//&检查摄像头是否支持录像
doesCameraSupportShootingVideos{
&&return&[self&cameraSupportsMedia:(&NSString&*)kUTTypeMoviesourceType:UIImagePickerControllerSourceTypeCamera];
//&检查摄像头是否支持拍照
doesCameraSupportTakingPhotos{
&&return&[self&cameraSupportsMedia:(&NSString&*)kUTTypeImagesourceType:UIImagePickerControllerSourceTypeCamera];
#pragma mark - 相册文件选取相关
相册是否可用
isPhotoLibraryAvailable{
& return [UIImagePickerController
isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary];
是否可以在相册中选择视频
canUserPickVideosFromPhotoLibrary{
& return [self
cameraSupportsMedia:( NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
是否可以在相册中选择视频
canUserPickPhotosFromPhotoLibrary{
& return [self
cameraSupportsMedia:( NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
三、用摄像头进行拍照和录像功能
1.我们将UIImagePickerController功能写在一个按钮的点击事件中:
#pragma mark -
拍照按钮事件
- (void)ClickControlAction:(id)sender{
判断有摄像头,并且支持拍照功能
isCameraAvailable] &&
doesCameraSupportTakingPhotos]){
& & // 初始化图片选择控制器
UIImagePickerController *controller =
[[UIImagePickerController alloc]
& & [controller
setSourceType:UIImagePickerControllerSourceTypeCamera];//
设置所支持的类型,设置只能拍照,或则只能录像,或者两者都可以
& & NSString *requiredMediaType = (
NSString *)kUTTypeImage;
& & NSString *requiredMediaType1 = (
NSString *)kUTTypeMovie;
& & NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType,
requiredMediaType1,nil];
& & [controller setMediaTypes:arrMediaTypes];
& & // 设置录制视频的质量
& & [controller
setVideoQuality:UIImagePickerControllerQualityTypeHigh];
& & //设置最长摄像时间
& & [controller setVideoMaximumDuration:10.f];
& & [controller setAllowsEditing:YES];//
设置是否可以管理已经存在的图片或者视频
& & [controller setDelegate:self];//
& & [self.navigationController
presentModalViewController:controller animated:YES];
& & [controller release];
& & NSLog(@"Camera is not available.");
2.&setSourceType方法
通过设置setSourceType方法可以确定调用出来的UIImagePickerController所显示出来的界面
typedef&NS_ENUM(NSInteger,
UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
分别表示:图片列表,摄像头,相机相册
3.setMediaTypes方法
//&设置所支持的类型,设置只能拍照,或则只能录像,或者两者都可以
&&NSString&*requiredMediaType
= (&NSString&*)kUTTypeImage;
&&NSString&*requiredMediaType1
= (&NSString&*)kUTTypeMovie;
&&NSArray&*arrMediaTypes=[NSArray&arrayWithObjects:requiredMediaType,
requiredMediaType1,nil];
& [controller&setMediaTypes:arrMediaTypes];
4.关于UIImagePickerControllerDelegate协议
我们要对我们拍摄的照片和视频进行存储,那么就要实现UIImagePickerControllerDelegate协议的方法。
#pragma mark - UIImagePickerControllerDelegate 代理方法
保存图片后到相册后,调用的相关方法,查看是否保存成功
imageWasSavedSuccessfully:(UIImage *)paramImage
didFinishSavingWithError:(NSError *)paramError
contextInfo:(void
*)paramContextInfo{
& & if (paramError == nil){
& & NSLog(@"Image was saved
successfully.");
& & } else {
& & NSLog(@"An error happened while saving the
& NSLog(@"Error
= %@", paramError);
当得到照片或者视频后,调用该方法
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{
& NSLog(@"Picker returned
successfully.");
& & NSLog(@"%@", info);
& NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
& // 判断获取类型:图片
& & if ([mediaType isEqualToString:( NSString *)kUTTypeImage]){
*theImage = nil;
& & // 判断,图片是否允许修改
& if ([picker
allowsEditing]){
& //获取用户编辑之后的图像
& theImage = [info objectForKey:UIImagePickerControllerEditedImage];
& } else {
照片的元数据参数
& theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
& & // 保存图片到相册中
selectorToCall = @selector(imageWasSavedSuccessfully:didFinishSavingWithError:contextInfo:);
& UIImageWriteToSavedPhotosAlbum(theImage,
self,selectorToCall,
& & }else if
([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
& & // 判断获取类型:视频
& & //获取视频文件的url
& & NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
& & //创建ALAssetsLibrary对象并将视频保存到媒体库
Library 框架包是提供了在应用程序中操作图片和视频的相关功能。相当于一个桥梁,链接了应用程序和多媒体文件。
& ALAssetsLibrary *assetsLibrary =
[[ALAssetsLibrary alloc] init];
& & // 将视频保存到相册中
& & [assetsLibrary
writeVideoAtPathToSavedPhotosAlbum:mediaURL
completionBlock:^(NSURL *assetURL, NSError *error) {
& & if (!error) {
& NSLog(@"captured video saved with no
& & }else{
& NSLog(@"error
occured while saving the video:%@", error);
& [assetsLibrary release];
[picker dismissModalViewControllerAnimated:YES];
当用户取消时,调用该方法
(void)imagePickerControllerDidCancel:(UIImagePickerController
[picker dismissModalViewControllerAnimated:YES];
四、从相册获取图片和视频数据
1.我们将功能封装在一个按钮的点击事件中
#pragma mark - 相册操作
(void)ClickShowPhotoAction:(id)sender{
([self isPhotoLibraryAvailable]){
UIImagePickerController *controller =
[[UIImagePickerController alloc]
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];//
& NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
& & if ([self
canUserPickPhotosFromPhotoLibrary]){
[mediaTypes addObject:(
NSString *)kUTTypeImage];
& & if ([self
canUserPickVideosFromPhotoLibrary]){
[mediaTypes addObject:(
NSString *)kUTTypeMovie];
& [controller setMediaTypes:mediaTypes];
& [controller setDelegate:self];//
& & [self.navigationController
presentModalViewController:controller animated:YES];
& [controller release];
& [mediaTypes release];
2.关于UIImagePickerControllerDelegate协议,我们可以重用。
在这里,就不用赘述了!
最后,需要说的是,UIImagePickerControllerDelegate协议中
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary
*)info方法,中的info值,会根据我们操作的类型不同,而产生了不同的数据信息:
当操作的为图片时::
UIImagePickerControllerCropRect = "NSRect: {{0, 405}, {2448,
UIImagePickerControllerEditedImage = "";
UIImagePickerControllerMediaMetadata = &
& DPIHeight = 72;
& DPIWidth = 72;
& Orientation = 6;
& "{Exif}" = & &
ApertureValue = "2.588";
BrightnessValue = "-0.6263";
& & & ColorSpace
DateTimeDigitized = " 16:43:00";
DateTimeOriginal = " 16:43:00";
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.76471";
& & & FNumber =
& & & Flash =
FocalLenIn35mmFilm = 35;
FocalLength = "4.28";
ISOSpeedRatings = & &
MeteringMode = 5;
PixelXDimension = 3264;
PixelYDimension = 2448;
& & & SceneType
SensingMethod = 2;
& & & Sharpness
ShutterSpeedValue = "4.131";
SubjectArea = & &
WhiteBalance = 0;
& "{TIFF}" = & &
& & & DateTime =
" 16:43:00";
& & & Make =
& & & Model =
"iPhone 4S";
& & & Software =
XResolution = 72;
YResolution = 72;
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "";
当我们操作的为视频时:
UIImagePickerControllerMediaType = "public.movie";
UIImagePickerControllerMediaURL =
"file://localhost/private/var/mobile/Applications/22A14825-DD7E-48E1-A1D5-2D85B82095B5/tmp/capture-T0x1363a0.tmp.etXfD4/capturedvideo.MOV";
希望对你有所帮助!
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。How to get the custom overlay for UIImagePicker camera to be full screen in iOS 7? [如何得到uiimagepicker相机在iOS 7全屏幕自定义覆盖?] - 问题-字节技术
How to get the custom overlay for UIImagePicker camera to be full screen in iOS 7?
如何得到uiimagepicker相机在iOS 7全屏幕自定义覆盖?
问题 (Question)
I am interested in using a custom overlay for the UIImagePickerController for camera controls that take the entire screen, very similar to what the default Camera app does when switched to the video function.
I have referenced several questions here on SO on this topic, particularly this , as well as the
on how to use a custom overlay for the UIImagePickerController, but none of them have been able to produce a successful, full-screen result with iOS 7.
So far, this is what the camera UI looks like with the custom overlay (without any buttons):
Note that there is a black bar in the bottom of the screen. I noticed that I could get the black bar removed if I change the cameraAspectRatio to 1, but this distorts the camera zoom as it then has a very zoomed in view. Is there a way to have full screen without either distorting the zoom too much (I understand that a small bit is necessary), and also remove the black bar?
Here is my code that configures the custom overlay:
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].
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);
An additional issue that I noticed is, the picture that is shown in camera preview is always smaller and has less details than the picture that is saved when the method [self.imagePickerController takePicture].
To illustrate:
This is what the keyboard looks like in the camera preview with the black bar below (sorry its a bit shaky):
However, note that the actual captured image in the preview panel has a lot more details as shown here, especially towards the top, as well as both left and right.
My question is, how would be able to set my camera preview so that what the user sees in preview is exactly the image that it will capture and could be shown to them afterwards?
Here is the entire code in viewDidLoad that sets up the camera controls.
- (void)viewDidLoad
[super viewDidLoad];
//Appearance configuration
self.navigationItem.hidesBackButton = YES;
//UIImagePickerController initialization and setup
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate =
self.imagePickerController.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeC //if there is a camera avaliable
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoL//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.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].
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);
And in viewWillAppear, I call the image picker view:
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];
我在使用一个自定义的覆盖感兴趣UIImagePickerController相机控制,把整个屏幕,非常类似于默认的摄像机的应用程序时,切换到视频功能。我引用的几个问题在等这个话题,尤其是这个,以及如何使用自定义覆盖的uiimagepickercontroller,但是没有一个能够产生一个成功的,与iOS 7全屏幕的结果。到目前为止,这就是相机UI看起来和自定义覆盖(没有任何按钮):需要注意的是在屏幕的底部有一个黑色的酒吧。我注意到我可能拿黑色栏中删除如果我改变cameraAspectRatio1,但这扭曲了相机的变焦在当时有一个非常镜头。有没有办法全屏幕没有扭曲放大的太多(我明白一点是必需的),和黑网吧也删除?这里是我的代码,配置自定义覆盖:{
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].
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);
更新我注意到另一个问题是,图片,是在拍照预览显示的是永远较小的具有较小的细节比图片,当保存方法[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.imagePickerController.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeC //if there is a camera avaliable
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoL//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.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].
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);
在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];
最佳答案 (Best Answer)
After many attempts, this is what worked for me with many thanks to other people's suggestions.
The following facts were very helpful to know and keep in mind:
The camera's points resolution is 426 * 320. In order for the camera preview's height to be stretched to the phone's screen height of 568, it needs to be multiplied by a factor of 1.3333 when using CGAffineTransformScale.
Note that the below are hard coded with various numbers based on the iPhone 5's screen resolution in points. They could be improved by using such objects such as screen.height, screen.width and other variables to make it applicable to iPhone 4/4s dimensions as well.
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
//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 =
CGAffineTransform scale = CGAffineTransformScale(translate, 1..333333);
self.imagePickerController.cameraViewTransform =
经过多次的尝试,这是什么工作,我有许多感谢别人的建议。下面的事实进行了解和记住很有帮助:相机的积分分辨率426×320。为了使相机预览的高度是拉伸至568的手机屏幕的高度,它需要乘以1.3333时使用的一个因素CGAffineTransformScale。请注意,以下是硬的和基于点的iPhone 5的屏幕分辨率不同的数字编码。他们可以通过使用如screen.height等对象的改进,screen.width和其他变量,使其适用于iPhone 4 / 4维度及。
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
//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 =
CGAffineTransform scale = CGAffineTransformScale(translate, 1..333333);
self.imagePickerController.cameraViewTransform =
答案 (Answer) 2
I don't know why you use screen size. Just try this simple code:
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeC
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
controller.delegate =
[self presentViewController:controller animated:NO completion:^{}];
我不知道为什么你使用屏幕尺寸。试试这个简单的代码:if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeC
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
controller.delegate =
[self presentViewController:controller animated:NO completion:^{}];
答案 (Answer) 3
Because the screen aspect ratio is different than the 4/3 ratio of the camera itself, you need (as you mentioned) to slightly crop the edges in order to have the image extend to the bottom.
In order to do that, you need to have the width be wide enough so that the height can be the full screen.
So, your image width and height must be:
float cameraAspectRatio = 4.0 / 3.0; //! Note: 4.0 and 4.0 works
float imageWidth = screenSize.height / cameraAspectR
You'll also probably want the view presenting the camera image to be that width that extends off both sides of the screen.
因为屏幕宽高比是不同的4 / 3的相机本身比,你需要(正如你所提到的)稍微作物的边缘以图像的底部延伸到。为了做到这一点,你需要有足够宽的宽度,高度可全屏。所以,你的图像的宽度和高度必须是:float cameraAspectRatio = 4.0 / 3.0; //! Note: 4.0 and 4.0 works
float imageWidth = screenSize.height / cameraAspectR
你也可能想要查看呈现摄像机的图像是宽度,延伸了银幕两边。
答案 (Answer) 4
Make sure that you account for 20px status bar change in iOS7. If you are facing a 20px black screen at bottom of the screen then this will be your issue. You can check that whether the app is running in ios7 or not by one of these preprocessors
#define SYSTEM_VERSION_EQUAL_TO(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
And you can make following changes to your code and see if it works
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].
int heightOffset = 0;
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
heightOffset = 20;
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);
Please let me know if it works. I haven't coded it to test.
确保你的账户中ios7 20px状态栏的变化。如果你面对的是一个20px黑屏在屏幕底部,然后这将是你的问题。你可以检查是否应用在ios7或不由其中一个预处理器运行#define SYSTEM_VERSION_EQUAL_TO(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)
([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
你可以作如下改动你的代码是否有用{
self.imagePickerController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.
self.imagePickerController.cameraOverlayView = self.overlayV
self.overlayView =
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].
int heightOffset = 0;
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
heightOffset = 20;
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);
请让我知道,如果它的作品。我没有编码到测试。
答案 (Answer) 5
Try this code to maintain your resultant image in custom size. I got result by using custom method for getting resultant image as custom size.
First create IBOutlet for UIImageview.
-(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];
//----------------------------------------
imageview.image = //------------- additional method for custom image size
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
//---- Resize the original image by using custom method----------------------
-(void)resizeImage
UIImage *resizeImage = imageview.
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 /
float heightRatio = resizeImage.size.height /
float divisor = widthRatio & heightRatio ? widthRatio : heightR
width = resizeImage.size.width /
height = resizeImage.size.height /
rect.size.width
rect.size.height =
//indent in case of width or height difference
float offset = (width - height) / 2;
if (offset & 0) {
rect.origin.y =
rect.origin.x = -
[resizeImage drawInRect: rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageview.image = smallI
imageview.contentMode = UIViewContentModeScaleAspectF
试试这个代码自定义大小保持你的图像。我使用自定义的方法得到的图像作为自定义大小有结果。首先创建IBOutlet为UIImageview。-(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];
//----------------------------------------
imageview.image = //------------- additional method for custom image size
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
/ / -----原始图像的大小通过使用自定义的方法-----------------------(void)resizeImage
UIImage *resizeImage = imageview.
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 /
float heightRatio = resizeImage.size.height /
float divisor = widthRatio & heightRatio ? widthRatio : heightR
width = resizeImage.size.width /
height = resizeImage.size.height /
rect.size.width
rect.size.height =
//indent in case of width or height difference
float offset = (width - height) / 2;
if (offset & 0) {
rect.origin.y =
rect.origin.x = -
[resizeImage drawInRect: rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageview.image = smallI
imageview.contentMode = UIViewContentModeScaleAspectF
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:}

我要回帖

更多关于 outcrop 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信