2

在我的应用程序中,我从 iPhone 相机拍摄照片并将这些照片放入带有图像的 ScrollView 中。

问题是它只适用于我的情况,如果我设置

picker.allowsEditing = YES

但在这种情况下,照片将仅以正方形形式保存(感谢 allowEditing 是相机功能中的一个选择正方形)。

如果我取出这行代码(我想要的是完整的照片,而不仅仅是选定的正方形),那么占位符图像将被任何内容替换。

以下是所需的方法:

- (IBAction)takePhoto:(UIButton *)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImageView *image = (UIImageView *)[self.view viewWithTag:[self getCurrentPageNumber]];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
image.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];}

-(void)addImageToImageScrollView:(CGFloat)x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)]; 
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
imgView.image = [UIImage imageNamed:@"placeholder.png"];
imgView.contentMode = UIViewContentModeScaleAspectFit;
[imgView setTag:([self getCurrentPageNumber]+1)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(imageScrollViewButtonPressed:) forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
[button setTag:([self getCurrentPageNumber]+1)];    
[view addSubview:imgView];
[view addSubview:button];
[imageScrollView addSubview:view];}
4

1 回答 1

2

它与 UIImagePickerControllerDelegate 协议有关

当您设置allowsEditing为 yes 时,当委托方法触发时,您将开始收到一个充满有用信息的 NSDictionary:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

当您打电话时,您正在使用其中的一些信息UIImage *chosenImage = info[UIImagePickerControllerEditedImages];

by setting allowsEditing to NO the delegate method will pass nil in for the info argument.

You can confirm this by setting a breakpoint in your method, stepping in and observing that the info will be nil, implying that you are setting your *chosenImage为零。

于 2013-10-31T13:45:00.487 回答