在iOS

时间:2016-07-26 16:43:26

标签: ios uiimageview uiimage uiimagepickercontroller

我正在我的应用程序中创建一个允许您拍照的视图,然后将自动在该图像周围绘制一个框架。我面临的问题是,当我这样做时,顶部和侧面周围有一些空白区域。这是我的代码:

更新:如果我在6S中运行它,您将获得如下所示的图像。如果在6S Plus中,您将获得最终图像。

-(void)viewDidLoad {
    UIBarButtonItem *takePhoto = [[UIBarButtonItem alloc] initWithTitle:@"Take Picture" style:UIBarButtonItemStylePlain target:self action:@selector(takingPhoto)];
    UIBarButtonItem *savePhoto = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveIt)];
    self.navigationItem.rightBarButtonItems = @[takePhoto, savePhoto];

}

- (void)takingPhoto{

    ipc = [[UIImagePickerController alloc] init];
    ipc.delegate = self;
    ipc.allowsEditing = YES;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:ipc animated:YES completion:NULL];
    }
    else
    {
        ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:ipc animated:YES completion:NULL];    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    self.imgView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self applyFilter];
    [picker dismissViewControllerAnimated:YES completion:nil];

}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)applyFilter {
    NSLog(@"Running");

    UIImage *borderImage = [UIImage imageNamed:@"IMG_8055.PNG"];

    NSData *dataFromImage = UIImageJPEGRepresentation(self.imgView.image, 1);

    CIImage *beginImage= [CIImage imageWithData:dataFromImage];

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *border =[CIImage imageWithData:UIImagePNGRepresentation(borderImage)];

    CIFilter *filter= [CIFilter filterWithName:@"CISourceOverCompositing"];  //@"CISoftLightBlendMode"];
    [filter setDefaults];
    [filter setValue:border forKey:@"inputImage"];

    [filter setValue:beginImage forKey:@"inputBackgroundImage"];

    CIImage *outputImage = [filter valueForKey:@"outputImage"];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *newImg = [UIImage imageWithCGImage:cgimg];

    self.imgView.image = newImg;

}

在模拟器中,它看起来像这样:

enter image description here

但是,当我保存后查看已保存的照片时,它看起来像这样:

enter image description here

为什么顶部和右侧有空白区域?

以上是上述6S Plus图像:

enter image description here

0 个答案:

没有答案