更新:图像已捕获但不会显示

UPDATED: Image is captured but will not display

我有一些代码可以在我的一个应用程序上运行;所以我复制了代码,进行了必要的更改(即 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];
}

更新: 在捕获图像的某个时刻,我收到此警告:

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

也许与此有关?我试图纠正警告,但无济于事。

我在 Google 和 SO 中寻找过类似问题,但一无所获。我已经为此工作了两天,决定是时候寻求帮助了。提前致谢。

标清

对我有用。也许从情节提要中删除 oBookImage 对您的 viewcontroller 的引用,然后重新绑定它。

试试这个代码

-(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;
 }

我也遇到了这个问题。经过几个小时的研究,我发现了这个:iOS8 Snapshotting a view that has not been rendered results in an empty snapshot

也许最后的回答对您有帮助?但愿如此。如果不是,那么它可能只是一个错误。