我正在尝试让相机预览占据我视图的一部分,而不是整个屏幕。这甚至可能吗?我是否必须使用类似AV Foundation Framework的内容来执行此操作?我是iOS开发的新手,所以任何指导都将非常感谢!谢谢!
答案 0 :(得分:1)
是的,您需要使用AVFoundation
。在过去,有些人(包括我自己)尝试使用UIImagePickerController
代替(通过自定义视图替换其view
属性),但它在iOS 7中不起作用(或者至少不会像你期望的那样工作)。 1}}开始时,指定自定义预览从来都不是记录的功能,所以没什么大不了的。
答案 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;
}