ios – VPImageCropperViewController API问题

我将分享我的图像到Instagram,但在分享之前需要用户裁剪自己的照片,所以我使用VP
ImageCropperViewController(
https://github.com/windshg/VPImageCropper)首先裁剪图像然后分享到Instagram但结果是缩放图像:

作物面积:

结果:

这是我的代码:

- (IBAction)shareIt:(id)sender {
    UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, NO, 0.0);
    [captureView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    VPImageCropperViewController *imgCropperVC = 
    [[VPImageCropperViewController alloc] 
     initWithImage:image
     cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width,self.view.frame.size.width) 
     limitScaleRatio:3.0];

    imgCropperVC.delegate = self;

    [self presentViewController:imgCropperVC animated:YES completion:nil];
}

VPImageCropperDelegate

- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {    
[cropperViewController dismissViewControllerAnimated:YES completion:^{

    NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];

    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {            
        NSURL *url;
        docFile.delegate = self;

        //Save image to directory
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.jpg"];

        NSData *imageData = UIImagePNGRepresentation(editedImage);
        [imageData writeToFile:savedImagePath atomically:NO];

        //load image
        NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.jpg"];
        UIImage *tempImage = [UIImage imageWithContentsOfFile:getImagePath];

        //Hook it with Instagram
        NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Image.ig"];
        [UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:jpgPath atomically:YES];

        url = [[NSURL alloc] initFileURLWithPath:jpgPath];
        docFile = [UIDocumentInteractionController interactionControllerWithURL:url];
        [docFile setUTI:@"com.instagram.photo"];
        docFile.annotation = @{@"InstagramCaption" : @" #geometrica" };
        [docFile presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];

    }else {        
        UIAlertView *errorToShare = [[UIAlertView alloc] initWithTitle:@"Instagram unavailable " message:@"You need to install Instagram" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [errorToShare show];
    }    
}];
}

最佳答案 看来这里的错误在于你的imgCropperVC的实例化.您告诉裁剪器从您提供的图像中裁剪出特定的矩形(带有黄色边框的大小).既然你在评论中说过,图片的大小与屏幕的大小相同,并且由于你的结果似乎只是宽度的一半(它直接通过他的脸部裁剪),我建议尝试

[[VPImageCropperViewController alloc] 
 initWithImage:image
 cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width*2,self.view.frame.size.width*2) 
 limitScaleRatio:3.0];

这可能与@ 2x有关,但我不确定这是否解决了其他图像的问题.

我怀疑原始图片比屏幕大,并缩小以适应它.如果这是正确的,那就会发生奇怪的事情(并非真的).

你的屏幕大小可能是640px宽.让我们假设原始图片(picture.jpg)的宽度为1280像素或其他东西.当你在ViewController或UIScrollView中的UIImageView中显示它时,它显然会缩放以适应,否则你将无法看到整个图片.如果您现在使用您的代码行,那么您要求裁剪器从宽度为1280像素的图片中裁剪出640的宽度(您自己的屏幕和矩形的宽度).裁剪器并不关心你的屏幕有多宽,它只能从1280像素宽的图像中裁剪掉640像素,因为这就是你告诉它要做的事情.这将导致图像被切成两半.如果你的图像按比例缩小(默认情况下,我猜),你需要在你的代码行中使用这个比例.如果原始图片实际上是1280px宽,那么上面的代码将起作用,因为我将矩形的大小加倍(视图的宽度大概是320,这与我认为的640视网膜相同).

如果将self.view.fram.size.width和height乘以图片缩放的量,则应获得正确的图像.这也适用于您发送的“静态”100.0f.对于你的VIEW它是100f,但如果图像按比例放大或缩小,这将是不正确的.您需要将此值乘以相同的值.我相信你可以从scrollView或imageView或其它东西获得缩放比例.

我建议您使用不同的图片尺寸测试您的应用.如果测试的像素尺寸与UIView完全相同,我认为它会完美运行,并会证实我的怀疑.

点赞