0

我对这种编码很陌生,但已经学到了一些东西,不幸的是在我正在开发的应用程序中遇到了障碍。

我正在尝试创建一个相机应用程序,允许用户拍摄“之前”图像,然后拍摄“之后”图像,然后我希望将图像放在一起(左和右)并保存为一张图片。

到目前为止,我已经设法使用以下代码对应用程序进行编码以拍摄单张照片:

标题:

#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

这是我需要添加的内容列表,非常感谢您对其中任何一个的帮助!

  • 向“After”按钮添加与“Before”相同的功能,但将图像存储在“afterImage”图像视图中。

  • 向“保存图像”按钮添加功能,将拍摄的两张图像保存为拼接在一起的一张图像。

谢谢大家!

4

1 回答 1

0

1. 第一个问题,我猜你的问题是“你怎么知道哪张照片是拍的,在哪里显示”?

Answer1:创建两个 UIImagePickerControllers 作为属性。一旦imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info返回图像,您就可以简单地检查它是哪个控制器if( picker == afterPictureController){...

答案 2:创建一个标志,指示下一张拍摄的照片是之后照片还是之前照片。它不是很灵活,但很有效。

2. 要拼接两个图像,您可以使用 size(image1Width+image2Width, MAX(image1Height, image2Height)) 创建 UIGraphicsBeginImageContext 并使用上下文绘制图像。之后,您可以通过调用从上下文中获取拼接图像UIGraphicsGetImageFromCurrentImageContext();

于 2013-10-10T14:14:25.310 回答