Problem with the VPImageCropperViewController API

I am going to share my image with Instagram, but before giving the user the option to crop their own photo, I used VPImageCropperViewController( https://github.com/windshg/VPImageCropper ) to crop the image first and then share it with Instagram, but the result by scaled image:

Cropping area:

enter image description here

and the result:

enter image description here

here are my codes:

- (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];
    }    
}];
}
+4
source share
2 answers

Finally, I will figure out how to crop the image size, there is a method in demonstrating the project:

- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize;

you can specify the size of your target images with it, for example:

image = [self imageByScalingAndCroppingForSourceImage:image targetSize:CGSizeMake(1024, 1024)];
0
source

, 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 - . UIImageView ViewController UIScrollView, , , , , . , 640 ( ) 1280 . , , 640 1280 , , . , . (, -, ), . 1280 , , ( , , 320, , 640 ).

self.view.fram.size.width height , , . "" 100.0f, . 100f, , . . , scrollView imageView - .

. , UIView, , , .

+2

Source: https://habr.com/ru/post/1529584/


All Articles