在我的应用程序中,单击UIButton时将打开相机。一旦相机打开,预览窗口会显示黑色而不是捕获的图像,但重新拍摄和使用照片按钮是可见的。这个黑色的预览在iPhone6的相机中显示,在iPhone 5、5s上正常工作。一旦用户点击使用照片按钮,我的应用程序将导航到另一个UIViewController。捕获的图像存储在变量中,并将传递到另一个UIViewController。从这个UIViewController它将被发送到服务器。我无法确定是什么原因导致了黑色的预览屏幕。希望得到帮助。以下是我的代码:
ON button click
{
[self takeNewPhotoFromCamera];
}
- (void)takeNewPhotoFromCamera
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = NO;
//controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
controller.delegate = self;
[self callOperationQue];
}
}
- (void)callOperationQue{
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.navigationController presentViewController: controller animated: YES completion: nil];
}];
}
else
{
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
UIImage *image1 = [info valueForKey: UIImagePickerControllerOriginalImage];
// imageData = UIImagePNGRepresentation(image1);
UIImage *newImage = [self squareImageWithImage:image1 scaledToSize:sz];
imgVwProfile.image=newImage;
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;
imageData = UIImageJPEGRepresentation(newImage, compression);
while ([imageData length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1;
imageData = UIImageJPEGRepresentation(newImage, compression);
}
//passing image data to other UIViewController
CreateClaimViewController *address=[[CreateClaimViewController alloc]init];
address.img=newImage;
[self.navigationController pushViewController:address animated:NO];
}
//resizing of image
- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
double ratio;
double delta;
CGPoint offset;
//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);
//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.width / image.size.width;
delta = (ratio*image.size.width - ratio*image.size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = newSize.width / image.size.height;
delta = (ratio*image.size.height - ratio*image.size.width);
offset = CGPointMake(0, delta/2);
}
//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * image.size.width) + delta,
(ratio * image.size.height) + delta);
//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
} else
{
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}