效果图:
建好一个新的 Single View Application 之后,在 ViewController 中加一个 UIImageView 和一个 button,connect 到 .h 文件中。
1. 关于picker.allowsEditing
reference里面的那个例子设置为YES,其结果就是可以截取所拍照片的一部分,对应的UIImage对象是 info[UIImagePickerControllerEditedImage]。
2.关于保存
使用:
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
这里chosenImage是要保存的UIImage对象。得到它的方法是:
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
3. 显示刚刚拍到的照片
self.imageView.image = chosenImage;
4. 关于一个bug
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Explanation: http://stackoverflow.com/questions/25884801/ios-8-snapshotting-a-view-that-has-not-been-rendered-results-in-an-empty-snapsho代码:
//
// ViewController.h
// CameraDemo
//
// Created by 鲁辰 on 7/5/15.
// Copyright (c) 2015 ChenLu. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)takePhoto:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *takePhotoButton;
@end
//
// ViewController.m
// CameraDemo
//
// Created by 鲁辰 on 7/5/15.
// Copyright (c) 2015 ChenLu. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.imageView.image = chosenImage;
//Save photo to camera roll
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
[self.view bringSubviewToFront:_takePhotoButton];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)takePhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
@end
Reference:
https://github.com/matthewmorton/Camera_Apphttp://stackoverflow.com/questions/14196012/how-to-force-a-uibutton-to-always-be-on-top
没有评论:
发表评论