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

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

Open
WenchaoD opened this issue Mar 3, 2015 · 3 comments
Open

Comments

@WenchaoD
Copy link

WenchaoD commented Mar 3, 2015

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?

@croath
Copy link
Owner

croath commented Apr 2, 2015

@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.

@WenchaoD
Copy link
Author

WenchaoD commented Apr 7, 2015

  • 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~

@croath
Copy link
Owner

croath commented Aug 26, 2016

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

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

2 participants