我对这种编码非常新颖但是已经学到了很多东西,并且不幸地在我正在开发的应用程序中遇到了麻烦。
我正在尝试创建一个相机应用程序,允许用户拍摄“之前”图像,然后拍摄“之后”图像,然后我会想要将图像放在一起的选项(左右)并保存为一个图像。
到目前为止,我已设法对应用进行编码以使用以下代码拍摄单张照片:
HEADER:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *beforeImage;
- (IBAction)cameraBeforeImage:(UIButton *)sender;
- (IBAction)libraryBeforeImage:(UIButton *)sender;
- (IBAction)deleteBeforeImage:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UIImageView *afterImage;
- (IBAction)cameraAfterImage:(UIButton *)sender;
- (IBAction)libraryAfterImage:(UIButton *)sender;
- (IBAction)deleteAfterImage:(UIButton *)sender;
- (IBAction)saveImages:(UIButton *)sender;
@end
实现:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)cameraBeforeImage:(UIButton *)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Oops!"
message:@"This Device Has No Camera, Please Select An Image From Your Library."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (IBAction)libraryBeforeImage:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)deleteAfterImage:(UIButton *)sender {
}
- (IBAction)saveImages:(UIButton *)sender {
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.beforeImage.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)deleteBeforeImage:(UIButton *)sender {
}
- (IBAction)cameraAfterImage:(UIButton *)sender {
}
- (IBAction)libraryAfterImage:(UIButton *)sender {
}
- (IBAction)saveImages:(UIButton *)sender {
}
@end
以下是我需要添加的内容列表,非常感谢任何帮助!
将相同的功能添加到'之前'按钮作为'之前',但将图像存储在'afterImage'图像视图中。
为'saveImages'按钮添加功能,将两张拍摄的图像保存为一个拼接在一起的图像。
提前谢谢大家!
答案 0 :(得分:0)
1。第一个问题,我想你的问题是'你怎么知道拍摄了哪张照片以及在哪里拍照'?
答案1:创建两个UIImagePickerControllers作为属性。一旦imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
返回图像,您只需检查哪个控制器是if( picker == afterPictureController){...
。
答案2:创建一个标志,指示下一张拍摄的照片是后照片还是前照片。它不是很灵活,但它有效。
2。要缝合两个图像,您可以创建大小为UIGraphicsBeginImageContext(image1Width + image2Width,MAX(image1Height,image2Height))并使用上下文绘制图像。之后,您可以通过调用UIGraphicsGetImageFromCurrentImageContext();