gpt4 book ai didi

ios - VPImageCropperViewController API 问题

转载 作者:可可西里 更新时间:2023-11-01 05:54:28 25 4
gpt4 key购买 nike

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

裁剪区域:

enter image description here

结果:

enter image description here

这是我的代码:

- (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 有关,但我不太确定这是否能解决您对其他图像的一般问题。

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

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

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

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

关于ios - VPImageCropperViewController API 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com