如何从方形边框裁剪图像

时间:2018-01-24 15:48:00

标签: ios objective-c uiscrollview uiimageview

我试图从我的图片选择器中删除所选图像(它是我的个人图书馆)..

我设法正确地切除了左侧和右侧但是我的顶部和底部都有问题..

简而言之,我无法正确剪切照片的顶部和底部......

我的scrollView里面有一个透明的正方形,就像图像剪辑器一样,为苹果提供了UIImagePicker。

必须裁剪我的图像,保持正方形的边缘

enter image description here 这是我正在使用的代码,你可以看到我有X源和图像高度的问题......你能帮我理解我错在哪里吗?

#pragma mark - Cropper Area
-(void)setupCropperArea {
    _cropper.backgroundColor = [UIColor clearColor];

    // Scroll View

    _scrollView = [[UIScrollView alloc] init];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    _scrollView.delegate = self;
    [self.cropper addSubview:_scrollView];

    [_scrollView.topAnchor constraintEqualToAnchor:self.cropper.topAnchor].active = YES;
    [_scrollView.leftAnchor constraintEqualToAnchor:self.cropper.leftAnchor].active = YES;
    [_scrollView.rightAnchor constraintEqualToAnchor:self.cropper.rightAnchor].active = YES;
    [_scrollView.bottomAnchor constraintEqualToAnchor:self.cropper.bottomAnchor].active = YES;


    // Image View

    _imageView = [[UIImageView alloc] init];
    _imageView.backgroundColor = [UIColor clearColor];
    _imageView.translatesAutoresizingMaskIntoConstraints = NO;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.scrollView addSubview:_imageView];

    _imageViewTopConstraint      = [_imageView.topAnchor constraintEqualToAnchor:self.scrollView.topAnchor];
    _imageViewLeadingConstraint  = [_imageView.leftAnchor constraintEqualToAnchor:self.scrollView.leftAnchor];
    _imageViewTrailingConstraint = [_imageView.trailingAnchor constraintEqualToAnchor:self.scrollView.trailingAnchor];
    _imageViewBottomConstraint   = [_imageView.bottomAnchor constraintEqualToAnchor:self.scrollView.bottomAnchor];

    _imageViewTopConstraint.active      = YES;
    _imageViewLeadingConstraint.active  = YES;
    _imageViewBottomConstraint.active   = YES;
    _imageViewTrailingConstraint.active = YES;


    // Square

    UIBezierPath *overlayPath = [UIBezierPath bezierPathWithRect:UIScreen.mainScreen.bounds];

    UIBezierPath *transparentPath = [UIBezierPath bezierPathWithRoundedRect:self.squareFrame cornerRadius:2];
    [overlayPath appendPath:transparentPath];

    CAShapeLayer *border = [CAShapeLayer layer];
    border.path = transparentPath.CGPath;
    border.fillRule = kCAFillRuleEvenOdd;
    border.fillColor = [UIColor clearColor].CGColor;
    border.strokeColor = [UIColor lightGrayColor].CGColor;
    border.lineWidth = 1;
    [self.cropper.layer addSublayer:border];

    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = overlayPath.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor colorWithHexString:@"#0F0F10" setAlpha:.3].CGColor;
    [self.cropper.layer addSublayer:fillLayer];
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
    [self centerContent];
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

-(CGFloat)squareX {
    return 10;
}

-(CGRect)squareFrame {
    CGRect screen = UIScreen.mainScreen.bounds;
    CGFloat squareSize = screen.size.width - (self.squareX * 2);
    CGFloat squareCenter =  (screen.size.height /2 - (squareSize /2));
    return CGRectMake(self.squareX, squareCenter, squareSize, squareSize);
}

-(CGFloat)landscapeMode {
    CGSize imageViewSize = self.imageView.frame.size;
    return imageViewSize.height < imageViewSize.width;
}

-(void)usesForCropping:(UIImage *)image {
    _imageView.image = image;

    CGFloat scaleHeight = self.scrollView.frame.size.height / image.size.height;
    CGFloat scaleWidth =  self.squareFrame.size.width / image.size.width;
    CGFloat scale = MIN(scaleWidth, scaleHeight);

    _scrollView.minimumZoomScale = scale;
    _scrollView.maximumZoomScale = 3;
    _scrollView.zoomScale = scale;

     [self centerContent];
}

-(void)centerContent {
    [self.view layoutIfNeeded];

    _scrollView.contentInset = UIEdgeInsetsZero;

    UIEdgeInsets scrollInset = self.scrollView.contentInset;

    CGFloat topInset     = self.squareFrame.origin.y - self.imageView.frame.origin.y;
    CGFloat bottomInset  = self.squareFrame.origin.y + self.imageView.frame.origin.y;
    CGFloat leftInset    = self.squareX - self.imageView.frame.origin.x;
    CGFloat rightInset   = self.squareX + self.imageView.frame.origin.x;

    CGSize  scrollSize   = self.scrollView.bounds.size;
    CGFloat imageWidth   = self.imageView.frame.size.width;
    CGFloat imageHeight  = self.imageView.frame.size.height;

    CGFloat centerHeight = (scrollSize.height - imageHeight) / 2;
    CGFloat centerWidth  = (scrollSize.width - imageWidth) /2;


    /* - - - - - Immagine scattata in LANDSCAPE - - - - - */
    if (self.landscapeMode){

        // Altezza Immagine SUPERIORE all'altezza del quadrato Guida
        if (imageHeight > self.squareFrame.size.height) {
            scrollInset.top    = topInset;
            scrollInset.bottom = bottomInset;
        }

        // Default
        else {
            _imageViewTopConstraint.constant    = centerHeight;
            _imageViewBottomConstraint.constant = centerHeight;
            scrollInset.top    = 0;
            scrollInset.bottom = 0;
        }

        // Larghezza Immagine SUPERIORE alla larghezza del quadrato Guida
        if (imageWidth > self.squareFrame.size.width) {

            scrollInset.left  = leftInset;
            scrollInset.right = rightInset;
        }

        // Default
        else {
            _imageViewLeadingConstraint.constant  = centerWidth;
            _imageViewTrailingConstraint.constant = centerWidth;
            scrollInset.left  = 0;
            scrollInset.right = 0;
        }

        NSLog(@"LANDSCAPE");
    }


    /* - - - - - Immagine scattata in PORTRAIT - - - - - */
    else {
        // Larghezza Immagine SUPERIORE alla larghezza del quadrato Guida
        if (imageWidth > self.squareFrame.size.width)
            scrollInset = UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset);

        // Larghezza Immagine INFERIORE alla larghezza del quadrato Guida
        else if (imageWidth < self.squareFrame.size.width)
            scrollInset = UIEdgeInsetsZero;

        // Default
        else  {
            scrollInset = UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset);
            _imageViewTopConstraint.constant      = centerHeight;
            _imageViewBottomConstraint.constant   = centerHeight;
            _imageViewLeadingConstraint.constant  = centerWidth;
            _imageViewTrailingConstraint.constant = centerWidth;
        }
        NSLog(@"PORTAIT");

    }

    _scrollView.contentInset = scrollInset;
    NSLog(@"%f", _scrollView.frame.size.height);
}

这是裁剪的代码

- (IBAction)selectAndCropImage:(id)sender {



    CGFloat scale = 1 / self.scrollView.zoomScale;

    CGFloat x = self.scrollView.contentOffset.x * scale;
    CGFloat width = (self.scrollView.frame.size.width - (self.squareX *2)) * scale;
    CGFloat y = ?;
    CGFloat height = ?;



    CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageView.image CGImage], CGRectMake(x, y, width, height));
    UIImage *resultImage = [UIImage imageWithCGImage:imageRef];

    [self.delegate didFinishPickImage:resultImage];
}

0 个答案:

没有答案