ios - Making an Image Picker with UIButton in Swift -
i make image picker uibutton pointing imagepickerview don't know how proceed.
we'll want connect viewcontroller "a" (with uibutton) viewcontroller "b" (uiimagepicker).
this tried : have storyboard multiples viewcontroller, , don't know why can't make ibaction viewcontroller "a" viewcontroller "b".
should make imagepickerview programatically or can use storyboard?
thanks in advance !
uiimagepickercontroller
can't added in storyboard, must programmatically. first, create ibaction uibutton:
- (ibaction)openimagepicker:(id)sender;
your class need adopt uiimagepickercontrollerdelegate
, uinavigationcontrollerdelegate
protocol:
@interface myviewcontroller () <uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate>
then show image picker in openimagepicker:
method:
// check if image source available // in case, photo library available. if use // uiimagepickercontrollersourcetypecamera instead, simulator doesn't have if ([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) { uiimagepickercontroller *picker = [[uiimagepickercontroller alloc] init]; // create image picker picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary; // choose source type picker.delegate = self; // delegate when image picked picker.allowsediting = yes; // allow user edit image before choose [self presentviewcontroller:picker animated:yes completion:nil]; // show image picker }
to use picked image, must implement method of uiimagepickercontrollerdelegate
:
/** * @param picker image picker * @param info dictionary contains picked images , metadata... */ - (void)imagepickercontroller:(nonnull uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nonnull nsdictionary *)info { // because allow user edit image, // choose uiimagepickercontrollereditedimage // can see other keys in uiimagepickercontroller class header uiimage *editedimage = [info objectforkey:uiimagepickercontrollereditedimage]; // dismiss picker dispatch_async(dispatch_get_main_queue(), ^{ [self dismissviewcontrolleranimated:yes completion:nil]; }); // use image here }
Comments
Post a Comment