ios - Downcasting UIImage to a UIImage subclass and calling method -


hi have subclass of uiimage called sasuploadimage.

here's sasuploadimage.h

#import <uikit/uikit.h>  @class device;  @interface sasuploadimage : uiimage  @property(nonatomic, weak) nsstring *timestamp;  @end 

here's .m

#import "sasuploadimage.h"  @implementation sasuploadimage  @synthesize timestamp;  @end 

so in class want set timestamp property. create property called sasuploadimage , try reference uiimage object. here's how it:

self.sasuploadimage = (sasuploadimage*)info[uiimagepickercontrolleroriginalimage]; 

info[uiimagepickercontrolleroriginalimage] <-- returns uiimage

now can't reference uiimage object , call settimestamp. that's why tried downcast (sasuploadimage*)

however, following error terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uiimage settimestamp:]: unrecognized selector sent instance 0x170086180'

i see why have error trying called unrecognised method, i'm not sure how downcast uiimage object sasuploadimage object. there way can make uiimage object sasuploadimage object?

this not way how type conversion works. info[uiimagepickercontrolleroriginalimage] returns instance of uiimage , must not cast sasuploadimage because internally uiimage instance, not sasuploadimage instance. can add custom initializer sasuploadimage class , create new instance image have, example:

.h file

#import <uikit/uikit.h>  @class device;  @interface sasuploadimage : uiimage  @property(nonatomic, weak) nsstring *timestamp;  - (instancetype)initwithimage:(uiimage *)img;  @end 

.m file

- (instancetype)initwithimage:(uiimage *)img {     return [super initwithcgimage:[img cgimage]] } 

and then

uiimage *img = info[uiimagepickercontrolleroriginalimage]; self.sasuploadimage = [[sasuploadimage alloc] initwithimage:img]; 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -