UIImageView-BetterFace
UIImageView-BetterFace copied to clipboard
Should the hack part been moved to the UIImageView+BetterFace.m ?
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?
@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 itsload
, 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.
- 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 aswitch
instead. When users need that feature, they callsetBetterfaceImage
, when they don't, they could just callsetImage
. As a result, thehack thing
is no longer needed, thesetBetterfaceImage
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 anycompatibility
work. Maybe the only limitation is that the class must be a NSObject subclass~
Could you please check out #13 , especially the readme file diff?