- (IBAction)TakePicture:(id)sender {
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Delegate is self
imagePicker.delegate = self;
OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
// imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
// Insert the overlay:
imagePicker.cameraOverlayView = overlay;
// Allow editing of image ?
imagePicker.allowsImageEditing = YES;
[imagePicker setCameraDevice:
UIImagePickerControllerCameraDeviceFront];
[imagePicker setAllowsEditing:YES];
imagePicker.showsCameraControls=YES;
imagePicker.navigationBarHidden=YES;
imagePicker.toolbarHidden=YES;
imagePicker.wantsFullScreenLayout=YES;
self.library = [[ALAssetsLibrary alloc] init];
// Show image picker
[self presentModalViewController:imagePicker animated:YES];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Save image to album
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// save image to custom album
[self.library saveImage:image toAlbum:@"custom name" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(@"Big error: %@", [error description]);
}
}];
[picker dismissModalViewControllerAnimated:NO];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
// After saving iamge, dismiss camera
[self dismissModalViewControllerAnimated:YES];
}
來源
2012-11-06 15:06:55
raj
感謝您的快速回復。但是,如何檢測哪個UIImageView被點擊?是否有一種方法可以在用戶點擊圖像時顯示彈出窗口,以便用戶選擇要保存的按鈕?當「保存」按鈕被按下時,我會把你的陳述放在IBAction中,對吧? – Brian 2010-11-16 19:47:45
您可以將Interface Builder中的UIImageView中的'touch up inside'事件連接到您實現的操作方法(這也可以通過編程方式完成),例如' - (void)touchedImageView:(id)sender',在這種情況下「發件人」將成爲被觸動的觀點。從那裏,你可以展示一個菜單(例如'UIActionSheet')來決定是否保存圖像。 – bosmacs 2010-11-16 20:05:41
我的代碼已關閉,但在界面生成器中沒有看到任何「觸摸」事件。選擇我的一個UIImageView,我進入Inspector窗口,然後是Connections選項卡,對不對?我看到按鈕被選中時的觸摸事件,但不是UIImageView。我應該只是將所有這些UIImageView放入按鈕中嗎? – Brian 2010-11-16 20:29:16