IOS獲取攝像和本地中的資源

-(IBAction)btnClick{
   UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                               initWithTitle:@"請選擇文件來源"
                                                    delegate:self
                                       cancelButtonTitle:@"取消"
                                destructiveButtonTitle:nil
                                       otherButtonTitles:@"照相機",@"攝像機",@"本地相簿",@"本地視頻",nil];
            [actionSheet showInView:self.view];
            [actionSheet release];
}

點擊按鈕觸發的btnClick事件後將會彈出一個以下的選擇筐: ide

接下來將要爲UIActionSheet實現其中的委託了。 spa

#pragma mark -
#pragma UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{   
    NSLog(@"buttonIndex = [%d]",buttonIndex);
    switch (buttonIndex) {
        case 0://照相機
            {10                 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = YES;
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
                [self presentModalViewController:imagePicker animated:YES];
                [imagePicker release];
            }
            break;
        case 1://攝像機
            {22                 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = YES;
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
                imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow;
                [self presentModalViewController:imagePicker animated:YES];
                [imagePicker release];
            }
            break;
        case 2://本地相簿
            {35                 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = YES;
                imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
                [self presentModalViewController:imagePicker animated:YES];
                [imagePicker release];
            }
            break;
        case 3://本地視頻
            {47                 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.allowsEditing = YES;
                imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
                [self presentModalViewController:imagePicker animated:YES];
                [imagePicker release];
            }
            break;
        default:
            break;
    }
}



實現了UIActionSheet的委託後,要發現,咱們使用了UIImagePickerController,這個類將幫咱們實現選取文件,打開對應的選取方式。好比當ButtonIndex爲0的時候,它將幫咱們打開照相機,咱們可使用相機拍攝照片做爲上傳的選取文件。所以,在這裏咱們還要實現UIImagePickerController的委託:

#pragma mark - 
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage  *img = [info objectForKey:UIImagePickerControllerEditedImage];
        self.fileData = UIImageJPEGRepresentation(img, 1.0);
    } else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {
        NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        self.fileData = [NSData dataWithContentsOfFile:videoPath]; 
    }
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{
    [picker dismissModalViewControllerAnimated:YES];
}



以後,你選取的文件便保存在了filedata中。就能夠隨時過來調用了。
相關文章
相關標籤/搜索