ios - How to write block definition using properties? -
i writing class getting image photos library. want 1 single method return selected image library. started writing class named mediabrowser. used block give selected image. confused write block definition. please correct code if going wrong.
in mediabrowser.h
@interface mediabrowser : nsobject typedef uiimage* (^mediabrowsercompletionhandler)(void); + (id)sharedinstance; - (bool)startmediabrowserfromviewcontroller:(uiviewcontroller*)controller completionhandler:(mediabrowsercompletionhandler)completion; @end
in mediabrowser.m
@interface mediabrowser () <uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate> @property (nonatomic, strong) mediabrowsercompletionhandler completionhandler; @end @implementation mediabrowser static mediabrowser *sharedmediabrowser = nil; + (id)sharedinstance { if (nil != sharedmediabrowser) { return sharedmediabrowser; } static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ sharedmediabrowser = [[mediabrowser alloc] init]; }); return sharedmediabrowser; } - (bool)startmediabrowserfromviewcontroller:(uiviewcontroller *)controller completionhandler:(mediabrowsercompletionhandler)completion { self.completionhandler = [completion copy]; if (([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypesavedphotosalbum] == no) || (controller == nil)) return no; uiimagepickercontroller *mediaui = [[uiimagepickercontroller alloc] init]; mediaui.sourcetype = uiimagepickercontrollersourcetypesavedphotosalbum; // displays saved pictures camera roll album. mediaui.mediatypes = [[nsarray alloc] initwithobjects:(nsstring *)kuttypeimage, nil]; // hides controls moving & scaling pictures, or // trimming movies. instead show controls, use yes. mediaui.allowsediting = no; mediaui.delegate = self; [controller presentmodalviewcontroller:mediaui animated:yes]; return yes; } // uiimagepickercontrollerdelegate - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { nsstring *mediatype = [info objectforkey: uiimagepickercontrollermediatype]; uiimage *imagetouse; // handle still image picked photo album if (cfstringcompare ((cfstringref) mediatype, kuttypeimage, 0) == kcfcompareequalto) { imagetouse = (uiimage *) [info objectforkey:uiimagepickercontrolleroriginalimage]; // imagetouse if (self.completionhandler) { // pass here uiimage self.completionhandler(); } } [picker dismissmodalviewcontrolleranimated:yes]; } @end
typedef uiimage* (^mediabrowsercompletionhandler)(void); @interface mediabrowser : nsobject @property (nonatomic,copy) mediabrowsercompletionhandler handler; @end
Comments
Post a Comment