GithubHelp home page GithubHelp logo

Comments (5)

croath avatar croath commented on August 29, 2024

@candyan maybe image size is too small.

from uiimageview-betterface.

candyan avatar candyan commented on August 29, 2024

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

from uiimageview-betterface.

crazytonyli avatar crazytonyli commented on August 29, 2024

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

from uiimageview-betterface.

liangdrime avatar liangdrime commented on August 29, 2024

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;
}

from uiimageview-betterface.

croath avatar croath commented on August 29, 2024

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

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

from uiimageview-betterface.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.