更新:图像已捕获但无法显示

5

我有一些与我的应用程序相关的代码;所以我复制了这些代码,并进行了必要的更改(例如textField名称等),它可以工作,但是当我将其移动到UIImageView时,什么也没有出现。这是我的代码:

viewController.h(只显示相关部分以保持简洁)

#import "AppDelegate.h"
#import "Books.h"
#import <AVFoundation/AVFoundation.h>
#import "MBProgressHUD.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <UIKit/UIKit.h>

@class Books;

@interface DetailViewController : UIViewController <UITextFieldDelegate,
    UIPopoverControllerDelegate,
    UIPickerViewDataSource,
    UIPickerViewDelegate,
    UIActionSheetDelegate,
    UIScrollViewDelegate,
    UIImagePickerControllerDelegate,
    AVCaptureMetadataOutputObjectsDelegate>  {
}

//  UIView
@property (strong, nonatomic) IBOutlet UIView *bookDetailView;

//  camera stuff
@property (strong, nonatomic) IBOutlet UIImageView *oBookImage;
- (IBAction)bOpenCamera:(UIButton *)sender;

以下是相关的viewController.m代码:

#pragma mark -  Camera stuff
- (IBAction)bOpenCamera:(UIButton *)sender {

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])  {

    UIImagePickerController *imagePicker = [UIImagePickerController new];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = [NSArray arrayWithObjects: (NSString *) kUTTypeImage, nil];
    imagePicker.allowsEditing = YES;  //  MUST HAVE!

    [imagePicker setDelegate: (id)self];
    [self presentViewController:imagePicker animated:YES completion:nil];
}

}

-(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo  {

if (error) {
    CommonMethods *cm = [CommonMethods new];
    [cm displayAlert:@"Warning!" andData:@"Failed to save book image..." andTag:0 andViewController:self];
}
}


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

self.oBookImage.contentMode = UIViewContentModeScaleAspectFill;

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:NO completion:nil];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {  //  (abstract image data)

    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];

    //  resize it...
    CGRect screenRect = CGRectMake(0, 0, 114.0, 128.0);  //  was 90.0, 120.0
    UIGraphicsBeginImageContext(screenRect.size);
    [image drawInRect:screenRect blendMode:kCGBlendModePlusDarker alpha:1];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    oBookImage.image = newImage;  //  show it...

}
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker  {

[self dismissViewControllerAnimated:NO completion:nil];
}

更新:在捕获图像时,我遇到了以下警告:

对尚未呈现的视图进行快照会导致空白快照。请确保您的视图至少已呈现一次后再进行快照或在屏幕更新后进行快照。

也许这与问题有关?我试图纠正警告,但没有成功。

我在Google和SO中寻找类似的问题,但没有找到任何信息。我已经花了两天时间来解决它,现在决定请求帮助。提前感谢。

SD


1
抱歉,你遇到了什么问题? - Tien
这个 if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 是真的吗? - J. Lopes
@J.Lopes 是的,就是这样... 所有应该执行的都已经执行了;我移动 newImage 到 UIImageView 的最后一条语句也被执行了,因为当我查看 oBookImage.image 时,图片会在短暂的时间内(以及在调试器中)出现,但当方法完成时,它就消失了! - SpokaneDude
1
你在 GitHub 上有这个项目吗? - Ro4ch
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - SpokaneDude
显示剩余8条评论
3个回答

2

我也遇到了这个问题。经过数小时的研究,我找到了这个链接:iOS8快照未渲染视图导致空白快照

也许最后一个答案能帮到你?我希望如此。如果不行,那可能只是个bug。


我也认为这是一个 bug,但我将尝试联系苹果技术支持,看看他们的说法。谢谢大家的建议。 - SpokaneDude

2

试试这段代码

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

self.oBookImage.contentMode = UIViewContentModeScaleAspectFill;

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {  //  (abstract image data)

    UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];

    oBookImage.image = [self resizeImage:image];  //  show it...

    }

[self dismissViewControllerAnimated:NO completion:nil];

}

-(UIImage *)resizeImage:(UIImage *)image
 {
      CGFloat compression = 0.9f;
      CGFloat maxCompression = 0.1f;
      int maxFileSize = 250*1024; // 250 KB

      while ([imageData length] > maxFileSize && compression > maxCompression)
      {
           compression -= 0.1;
           imageData = UIImageJPEGRepresentation(image, compression);
      }
       UIImage *img=[UIImage imageWithData:imageData];
       return img;
 }

没有任何区别,无论如何还是谢谢!SD - SpokaneDude
你的调整大小方法不是那样工作的,请展示我编辑过的代码。 - Chirag Kalsariya
你的 resizeImage 方法有两个主要错误:1)图像没有长度选择器,2)image 具有不兼容的指针类型。请在提交代码作为答案的一部分之前进行语法检查(构建)。SD - SpokaneDude
这段代码在我的项目中运行正常。 - Chirag Kalsariya

1

这对我很有效。也许从故事板中删除oBookImage到您的视图控制器的引用,然后重新绑定即可。


1
那没有起到任何作用...所以我设置了赏金...无论如何还是谢谢! - SpokaneDude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接