在我的应用程序中,我从iPhone相机拍摄照片,并将这些照片放入带有图像的ScrollView中。
问题是它仅适用于我的情况,如果我设置
picker.allowsEditing = YES
但在这种情况下,照片只会以正方形形式保存(感谢允许编辑是相机功能中的选择方块)。
如果我取出这行代码(我想要什么,因为我想要完整的照片,而不仅仅是它的选定部分),那么占位符图像就会被什么都没有替换。
以下是所需的方法:
- (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];}
它与 UIImagePickerControllerDelegate 协议有关
当您将allowsEditing
设置为 yes 时,您将开始收到一个充满有用信息的 NSDictionary:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
当您调用 UIImage *chosenImage = info[UIImagePickerControllerEditedImages];
by setting allowsEditing
以NO
委托方法将为info
参数传递 nil 时,您正在使用其中一些信息。
您可以通过在方法中设置断点来确认这一点,单步执行并观察info
将为 nil,这意味着您将 *selectedImage 设置为 nil。