TimOliver / TOCropViewController

A view controller for iOS that allows users to crop portions of UIImage objects
http://www.timoliver.com.au/2015/06/21/tocropviewcontroller-an-open-source-image-cropper-for-ios/
MIT License
4.73k stars 951 forks source link

Cropping With A Certain Rect At Start #17

Closed ahanmal closed 8 years ago

ahanmal commented 9 years ago

Hi,

I've been modifying TOCropViewController so I can initialize it with a custom rect and am having some trouble doing that. I modified all the initializers and everything to accept the CGRect but I have no clue where to set it. I'm sure that this needs to happen in TOCropView, but am unsure. I've tried setting some instance variables in TOCropView and also calling setCropBoxFrame after initialization to no avail. Any ideas?

Thanks, Ahan

TimOliver commented 9 years ago

Hi Ahan!

Hrmm, okay. What size do you want to set it to specifically? Would this be better solved by setting an initial aspect ratio, or do you need an explicit size?

The default crop box is set up in the layoutInitialImage method inside TOCropView, so if you can add some hooks to interact with the crop box each time after that one is called, that SHOULD let you do it.

arvindkumar-gr commented 9 years ago

You can use the below code to start with custom rect.

#pragma mark - Image Picker Delegate -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self dismissViewControllerAnimated:YES completion:^{
        self.image = image;
        TOCropViewController *cropController = [[TOCropViewController alloc] initWithImage:image];
        cropController.delegate = self;
        [self presentViewController:cropController animated:YES completion:nil];
        cropController.toolbar.clampButtonHidden = YES;
        cropController.toolbar.rotateButtonHidden = YES;
        [cropController.cropView setAspectLockEnabledWithAspectRatio:CGSizeMake(1.74f, 2.20f) animated:NO];
    }];
}
ahanmal commented 9 years ago

@arvindkumar-gr That's interesting. I didn't think about modifying the cropView directly. But, doesn't that only change the aspect ratio, and not set a starting origin for the cropView?

I've been trying to do it with the following:

    frame.origin.x = floor((self.baseCropBoxFrame.origin.x/self.imageSize.width)*bounds.size.width*0.5f);
    frame.origin.y = floor((self.baseCropBoxFrame.origin.y/self.imageSize.height)*bounds.size.height*0.5f);
    frame.size.width = floor((self.baseCropBoxFrame.size.width/self.imageSize.width)*bounds.size.width);
    frame.size.height = floor((self.baseCropBoxFrame.size.height/self.imageSize.height)*bounds.size.height);
    self.cropBoxFrame = frame;

Where baseCropBoxFrame is passed in during initialization and the one I want the crop frame to be. However, while it does adjust the crop frame, it's not the one I want. It has a different aspect ration. @TimOliver any reasons for the 0.5f? Where is the origin that I should be making the crop frame in terms of? Thanks!

TimOliver commented 8 years ago

Um. Hang on. So you want to make a specific subregion of the image selected by default?

Um, I'm still not sure of what effect you want to create, but setting the cropBoxFrame property only specifies the placement of the crop box as it appears on-screen, in no relation to the position to the actual image.

The image itself is controlled by the contentOffset and contentZoom properties of its parent UIScrollView.

TimOliver commented 8 years ago

Closing this as it has gone unreplied for nearly 3 weeks.