1. 程式人生 > >系統相冊和拍照

系統相冊和拍照

media nbsp string alloc 多媒體 str iat 方法 svi

1.系統相冊和拍照

//有系統預置的多媒體參數
#import <MobileCoreServices/MobileCoreServices.h>

UIImagePickerController 拍照和獲取系統相冊
推斷是否支持
BOOL ret = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

//設置資源類型
imagePicker.sourceType = type;
//設置代理
imagePicker.delegate = self;
//是否同意對相冊 等做興許處理
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:^{

}];


#pragma mark - UIImagePickerControllerDelegate
//點擊picker上的cancelbutton時,觸發的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"cancel!!");
//實現picker的dismiss
[picker dismissViewControllerAnimated:YES completion:^{
}];
}


//點擊choosebutton觸發的方法
//info 帶有選中資源的信息
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

//推斷選中的資源的類型
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//kUTTypeImage 系統預置的圖片資源類型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//證明取出來的是圖片
//通過字典獲取選中的圖片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//從相機中取出來的圖片占的空間:(1M-2M)左右,須要對圖片進行壓縮處理,然後在進行興許操作
//將原圖壓縮成50*50的尺寸
UIImage *smallImage = [[ImageTool shareTool] resizeImageToSize:CGSizeMake(50,50) sizeOfImage:image];
self.view.backgroundColor = [UIColor colorWithPatternImage:smallImage];
}
[picker dismissViewControllerAnimated:YES completion:^{
}];
}

系統相冊和拍照