IOS gets video and local resources

Source: Internet
Author: User

When uploading files, we select from the local device or use a camera to photograph the files.

Upload button, click the event

 1 - (Ibaction) btnclick {  2 Uiactionsheet * actionsheet = [[Uiactionsheet alloc]  3 Initwithtitle: @"  Select File Source  "  4                                                       Delegate  : Self  5 Cancelbuttontitle:@"  Cancel  "  6   Destructivebuttontitle: Nil  7 Otherbuttontitles: @"  Camera  " , @"  Camera  " , @"  Local album  " ,@"  Local Video  "  , Nil];  8   [Actionsheet showinview: Self. View];  9   [Actionsheet release];  10 }

After you click the button to trigger the btnclick event, the following selection basket will pop up:

The delegate is implemented for uiactionsheet.

 1   # Pragma Mark- 2   # Pragma Uiactionsheet delegate3 -( Void ) Actionsheet :( uiactionsheet * ) Actionsheet clickedbuttonatindex :( nsinteger) buttonindex  4   {  5 Nslog ( @"  Buttonindex = [% d]  "  , Buttonindex );  6       Switch  (Buttonindex ){  7           Case  0 : //  Camera  8   {  10 Uiimagepickercontroller * imagepicker = [[Uiimagepickercontroller alloc] init];  11 Imagepicker. Delegate = Self;  12 Imagepicker. allowsediting = Yes;  13 Imagepicker. sourcetype = Uiimagepickercontrollersourcetypecamera;  14 Imagepicker. mediatypes = [[nsarray alloc] initwithobjects: (nsstring * ) Kuttypeimage, nil];  15   [Self presentmodalviewcontroller: imagepicker animated: Yes];  16   [Imagepicker release];  17   }  18               Break  ; 19           Case   1 : //  Camera  20   {  22 Uiimagepickercontroller * imagepicker = [[Uiimagepickercontroller alloc] init];  23 Imagepicker. Delegate = Self;  24 Imagepicker. allowsediting =Yes;  25 Imagepicker. sourcetype = Uiimagepickercontrollersourcetypecamera;  26 Imagepicker. mediatypes = [[nsarray alloc] initwithobjects: (nsstring * ) Kuttypemovie, nil];  27 Imagepicker. videoquality = Uiimagepickercontrollerqualitytypelow;  28   [Self presentmodalviewcontroller: imagepicker animated: Yes];  29  [Imagepicker release];  30   }  31               Break  ;  32           Case   2 : //  Local album  33   {  35 Uiimagepickercontroller * imagepicker = [[Uiimagepickercontroller alloc] init]; 36 Imagepicker. Delegate = Self;  37 Imagepicker. allowsediting = Yes;  38 Imagepicker. sourcetype = Uiimagepickercontrollersourcetypephotolibrary;  39 Imagepicker. mediatypes = [[nsarray alloc] initwithobjects: (nsstring * ) Kuttypeimage, nil];  40  [Self presentmodalviewcontroller: imagepicker animated: Yes];  41   [Imagepicker release];  42   }  43               Break  ;  44           Case   3 : //  Local Video  45   { 47 Uiimagepickercontroller * imagepicker = [[Uiimagepickercontroller alloc] init];  48 Imagepicker. Delegate = Self;  49 Imagepicker. allowsediting = Yes;  50 Imagepicker. sourcetype = Uiimagepickercontrollersourcetypephotolibrary;  51 Imagepicker. mediatypes = [[nsarray alloc] initwithobjects: (nsstring *) Kuttypemovie, nil];  52   [Self presentmodalviewcontroller: imagepicker animated: Yes];  53   [Imagepicker release];  54   }  55               Break  ;  56           Default  :  57               Break  ; 58   }  59 }

After implementing the uiactionsheet delegate, we need to find that we use uiimagepickercontroller. This class will help us select files and open the corresponding selection method. For example, when the buttonindex is 0, it will help us open the camera, we can use the camera to take a photo as the upload of the selected file. Therefore, we need to implement the uiimagepickercontroller delegate here:

 1   # Pragma Mark- 2   # Pragma Uiimagepickercontroller delegate 3 -( Void ) Imagepickercontroller :( uiimagepickercontroller *) picker didfinishpickingmediawithinfo :( nsdictionary * ) Info 4   {  5       If ([[Info objectforkey: uiimagepickercontrollermediatype] isw.tostring :( nsstring * ) Kuttypeimage]) {  6 Uiimage * IMG = [Info objectforkey: uiimagepickercontrollereditedimage];  7 Self. filedata = uiimagejpegrepresentation (IMG, 1.0  );  8 } Else   If ([[Info objectforkey: uiimagepickercontrollermediatype] isw.tostring :( nsstring * ) Kuttypemovie]) {  9 Nsstring * videopath = [[Info objectforkey: uiimagepickercontrollermediaurl] path];  10 Self. filedata = [Nsdata datawithcontentsoffile: videopath];  11   }  12   [Picker dismissmodalviewcontrolleranimated: Yes];  13   }  14  15 -( Void ) Imagepickercontrollerdidcancel :( uiimagepickercontroller * ) Picker  16   {  17   [Picker dismissmodalviewcontrolleranimated: Yes];  18 }

Then, the selected file is saved in filedata. You can call it at any time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.