转载自:http://blog.csdn.net/l_ch_g/article/details/11738129点击打开链接
092 | UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; |
093 | if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) |
095 | UIImagePickerController *picker = [[UIImagePickerController alloc] init]; |
096 | picker.delegate = self; |
098 | picker.allowsEditing = YES; |
099 | picker.sourceType = sourceType; |
101 | [self presentModalViewController:picker animated:YES]; |
104 | NSLog(@ "模拟其中无法打开照相机,请在真机中使用" ); |
111 | UIImagePickerController *picker = [[UIImagePickerController alloc] init]; |
113 | picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; |
114 | picker.delegate = self; |
116 | picker.allowsEditing = YES; |
117 | [self presentModalViewController:picker animated:YES]; |
122 | -( void )imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info |
126 | NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; |
129 | if ([type isEqualToString:@ "public.image" ]) |
132 | UIImage* image = [info objectForKey:@ "UIImagePickerControllerOriginalImage" ]; |
134 | if (UIImagePNGRepresentation(image) == nil) |
136 | data = UIImageJPEGRepresentation(image, 1.0); |
140 | data = UIImagePNGRepresentation(image); |
145 | NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@ "Documents" ]; |
148 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
151 | [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil]; |
152 | [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@ "/image.png" ] contents:data attributes:nil]; |
155 | filePath = [[NSString alloc]initWithFormat:@ "%@%@" ,DocumentsPath, @ "/image.png" ]; |
158 | [picker dismissModalViewControllerAnimated:YES]; |
162 | UIImageView *smallimage = [[[UIImageView alloc] initWithFrame: |
163 | CGRectMake(50, 120, 40, 40)] autorelease]; |
165 | smallimage.image = image; |
167 | [self.view addSubview:smallimage]; |
173 | - ( void )imagePickerControllerDidCancel:(UIImagePickerController *)picker |
176 | [picker dismissModalViewControllerAnimated:YES]; |