Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

UIImage category not working properly #11

Open
crazytonyli opened this issue Mar 10, 2014 · 5 comments
Open

UIImage category not working properly #11

crazytonyli opened this issue Mar 10, 2014 · 5 comments

Comments

@crazytonyli
Copy link

用 UIImage+BetterFace 切这张图片没有切对。

@candyan 应该是 PR #4 的问题噢。

@croath
Copy link
Owner

croath commented Mar 10, 2014

@candyan maybe image size is too small.

@candyan
Copy link
Contributor

candyan commented Mar 27, 2014

有木有切不对的效果图捏。。

@crazytonyli
Copy link
Author

返回的结果就是原图,size 可以用 (160, 160) 试试。

@liangdrime
Copy link

This is a funny bug, I don't known what to say.
Because the writer only just copy the code from category of UIImageView to the category of UIImage.
So,bug comes. the parameter size in the category of UIImageView is size of image, but it means size of the image view. the size of image in category of UIImage is self.size.
And,there is a lower mistake, when calculate the frame by vertical, at last the operation to change the offset.y is -, not =(both in category of UIImageView)

 // offset.y = finalSize.height = size.height;
    offset.y = finalSize.height - size.height; 

The last bug is create the image. even though has moved the offset, but the size used to create image is the size of image view(the finalSize), not the size relative to the image. So, if the image is large ,the result image will be so small.
And, if you want to create a image to fit the image view, the size used to create image must have the W/H rate same to the image view, not the finalSize. Because in the category of UIImageView, it used layer to set image, and the layer can be the final size but has not bad effect to the image view.

At last, below is my code

- (UIImage *)subImageForFaceFeatures:(NSArray *)faceFeatures constrainedToSize:(CGSize)size {
    CGRect fixedRect    = CGRectMake(MAXFLOAT, MAXFLOAT, 0, 0);
    CGFloat rightBorder = 0, bottomBorder = 0;
    CGSize imageSize    = self.size;

    for (CIFaceFeature * faceFeature in faceFeatures){
        CGRect oneRect = faceFeature.bounds;
        // Mirror the frame of the feature
        oneRect.origin.y = imageSize.height - oneRect.origin.y - oneRect.size.height;

        // Always get the minimum x & y
        fixedRect.origin.x = MIN(oneRect.origin.x, fixedRect.origin.x);
        fixedRect.origin.y = MIN(oneRect.origin.y, fixedRect.origin.y);

        // Calculate the faces rectangle
        rightBorder = MAX(oneRect.origin.x + oneRect.size.width, rightBorder);
        bottomBorder = MAX(oneRect.origin.y + oneRect.size.height, bottomBorder);
    }

    // Calculate the size of rectangle of faces
    fixedRect.size.width = rightBorder - fixedRect.origin.x;
    fixedRect.size.height = bottomBorder - fixedRect.origin.y;

    CGPoint fixedCenter = CGPointMake(fixedRect.origin.x + fixedRect.size.width / 2.0,
                                      fixedRect.origin.y + fixedRect.size.height / 2.0);
    CGPoint offset = CGPointZero;
    CGSize finalSize = imageSize;
    if (imageSize.width / imageSize.height > size.width / size.height) {
        //move horizonal
        finalSize.height = size.height;
        finalSize.width = imageSize.width/imageSize.height * finalSize.height;

        // Scale the fixed center with image scale(scale image to adjust image view)
        fixedCenter.x = finalSize.width/imageSize.width * fixedCenter.x;
        fixedCenter.y = finalSize.width/imageSize.width * fixedCenter.y;

        offset.x = fixedCenter.x - size.width * 0.5;
        if (offset.x < 0) {
            // Move outside left
            offset.x = 0;
        } else if (offset.x + size.width > finalSize.width) {
            // Move outside right
            offset.x = finalSize.width - size.width;
        }

        // If you want the final image is fit to the image view, you should set the width adjust the image view.
        finalSize.width = size.width;
    } else {
        //move vertical
        finalSize.width = size.width;
        finalSize.height = imageSize.height/imageSize.width * finalSize.width;

        // Scale the fixed center with image scale(scale image to adjust image view)
        fixedCenter.x = finalSize.width/imageSize.width * fixedCenter.x;
        fixedCenter.y = finalSize.width/imageSize.width * fixedCenter.y;

        offset.y = fixedCenter.y - size.height * (1 - GOLDEN_RATIO);
        if (offset.y < 0) {
            // Move outside top
            offset.y = 0;
        } else if (offset.y + size.height > finalSize.height){
            // Move outside bottom
            // offset.y = finalSize.height = size.height;
            offset.y = finalSize.height - size.height;
        }

        // If you want the final image is fit to the image view, you should set the height adjust the image view.
        finalSize.height = size.height;
    }

    // The finalSize is just fit the image view now, so we should scale the frame to the image size.
    CGFloat scale = imageSize.width/finalSize.width;
    CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);
    // Get the final image rect
    CGRect finalRect = CGRectApplyAffineTransform(CGRectMake(offset.x, offset.y, finalSize.width, finalSize.height),transform);
    // Creat image
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], finalRect);
    UIImage *subImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(imageRef);

    return subImage;
}

@croath
Copy link
Owner

croath commented Aug 26, 2016

Hey guys sorry for the late response, would you please check out #13 ?

I used @Roylee-ML 's code for the replacement.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants