Skip to content

Should the hack part been moved to the UIImageView+BetterFace.m ? #12

Open

Description

@WenchaoD

Like this in UIImageView+BetterFace.m:

+ (void)load
{
hack_uiimageview_bf();
}

I use this code and success to parse the image in the demo.
I think it would be more intuitive and save the work to deal with "main.m".
Or is there a specific reason not to?

Activity

  1. croath commented on Apr 2, 2015

    @croath
    Owner

    @f33chobits Thank you for your issue, and sorry for the late reply.

    Yes there is a specific reason, like developer may not need this feature in some situation. For details:

    • If you put the hack_uiimageview_bf(); method into its load, developer may have no chance to turn off the feature for example of being in debugging mode.
    • I think it's better to run all method swizzling code at the very beginning.
    • I don't know if it's ok to override a +load method in the class's category
    • If you'd like to do it, you could checkout https://github.com/croath/UIImageView-BetterFace-Swift do something like this in Swift:
    extension UIImageView {
        public override class func initialize() {
            struct Static {
                static var token: dispatch_once_t = 0
            }
    
            dispatch_once(&Static.token) {
    // your swizzle code
            }
        }
    

    I hope my answer could make some sense.

  2. WenchaoD commented on Apr 7, 2015

    @WenchaoD
    Author
    • I understand you add hack_uiimageview_bf() as a switch to control whether users use this feature. But this is not a single switch, it's a global switch. When it's turned on, all UIImage instances will have that feature. As you have left an interface setBetterFaceImage out there, that could be a switch instead. When users need that feature, they call setBetterfaceImage, when they don't, they could just call setImage. As a result, the hack thing is no longer needed, the setBetterfaceImage could be like this:
    - (void)setBetterFaceImage:(UIImage *)image{
        [self setImage:image]; // instead of swizzling [self setBetterFaceImage:image]
        if (![self needsBetterFace]) {
            return;
        }
        [self faceDetect:image];
    }

    I hope this could make sense.

    • In objective-c it is ok to override a +load method in the class's category and do swizzling in it. But I used to do swizzling in swift +initialize as you mentioned and met some problem which could make app crash. I don't know whether it could work now, but it couldn't months ago. What I'm doing is just create objective-c class and do swizzling in +(void)load in Swift Project and it works fine, no need to import in bridge header, also no need to do any compatibility work. Maybe the only limitation is that the class must be a NSObject subclass~
  3. croath commented on Aug 26, 2016

    @croath
    Owner

    Could you please check out #13 , especially the readme file diff?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions