Created
April 29, 2014 05:35
-
-
Save interchen/11391330 to your computer and use it in GitHub Desktop.
camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (IBAction)handleUploadButton:(id)sender { | |
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear] == NO | |
&& [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] == NO) { | |
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:CustomLocalizedString(@"alert_cancel", nil) destructiveButtonTitle:nil otherButtonTitles:CustomLocalizedString(@"picker_photo", nil), nil]; | |
[sheet showFromRect:((UIButton *)sender).frame inView:self.view animated:YES]; | |
} else { | |
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:CustomLocalizedString(@"alert_cancel", nil) destructiveButtonTitle:CustomLocalizedString(@"picker_camera", nil) otherButtonTitles:CustomLocalizedString(@"picker_photo", nil), nil]; | |
[sheet showFromRect:((UIButton *)sender).frame inView:self.view animated:YES]; | |
} | |
} | |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
if (buttonIndex == actionSheet.cancelButtonIndex) { | |
return; | |
} | |
if (buttonIndex == actionSheet.destructiveButtonIndex) { | |
// 拍照 | |
[self setupImagePickerWithType:UIImagePickerControllerSourceTypeCamera]; | |
} else if (buttonIndex == actionSheet.firstOtherButtonIndex) { | |
[self setupImagePickerWithType:UIImagePickerControllerSourceTypePhotoLibrary]; | |
} | |
} | |
- (void)setupImagePickerWithType:(UIImagePickerControllerSourceType)type | |
{ | |
if (self.imagePickerController == nil) { | |
self.imagePickerController = [[UIImagePickerController alloc] init]; | |
} | |
self.imagePickerController.sourceType = type; | |
self.imagePickerController.allowsEditing = NO; | |
self.imagePickerController.delegate = self; | |
self.imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen; | |
if (type == UIImagePickerControllerSourceTypeCamera) { | |
[self presentViewController:self.imagePickerController animated:YES completion:nil]; | |
} else { | |
if (isiPad) { | |
if (self.imagePickerPopoverController == nil) { | |
self.imagePickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.imagePickerController]; | |
} else { | |
self.imagePickerPopoverController.contentViewController = self.imagePickerController; | |
} | |
[self.imagePickerPopoverController presentPopoverFromRect:self.uploadButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; | |
} else { | |
[self presentViewController:self.imagePickerController animated:YES completion:nil]; | |
} | |
} | |
} | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | |
{ | |
__block UIImage *image = nil; | |
NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL]; | |
if (referenceURL) { | |
//从图库选择 | |
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; | |
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset) { | |
image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]; | |
[self.uploadButton setImage:image forState:UIControlStateNormal]; | |
if (isiPad) { | |
[self.imagePickerPopoverController dismissPopoverAnimated:YES]; | |
} | |
[self uploadMapPicture]; | |
} failureBlock:^(NSError *error) { | |
// error handling | |
}]; | |
} else { | |
// 拍照 | |
image = [info objectForKey:UIImagePickerControllerOriginalImage]; | |
[self.uploadButton setImage:image forState:UIControlStateNormal]; | |
if (isiPad) { | |
[self.imagePickerPopoverController dismissPopoverAnimated:YES]; | |
} | |
[self uploadMapPicture]; | |
} | |
[picker dismissViewControllerAnimated:YES completion:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment