Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATED: Image is captured but will not display [duplicate]

I have some code that is working on one of my apps; so I copied the code, making necessary changes (ie textField names, etc) and it works except when I move it to the UIImageView; nothing appears. This is my code:

viewController.h (only relevant portion shown for brevity)

#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;

This is relevant viewController.m code:

#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];
}

UPDATE: at some point while capturing the image, I get this warning:

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.

Maybe this has something to do with it? I tried to correct the warning, to no avail.

I have looked for similar issues within Google and SO, but found nothing. I have been working on this for two days now and decided it's time to ask for help. Thanks in advance.

SD

like image 901
SpokaneDude Avatar asked May 14 '16 15:05

SpokaneDude


People also ask

Why won't my monitors duplicate display?

Restart everything: Shut down Windows and all your monitors. Then, turn everything on and boot up again. This can often fix your issue. If necessary, roll back driver updates: If your display driver recently updated, it could be causing the issue.

Why is duplicate not working?

Make sure that both monitors are using the same resolution Several users suggest that in order to duplicate your screen both monitors need to use the same resolution. Once both monitors are set to use the same resolution, the issue should be resolved and you'll be able to duplicate your screen without problems.


2 Answers

I, too have been getting this issue. After hours of researching, I've come across this: iOS8 Snapshotting a view that has not been rendered results in an empty snapshot

Perhaps the last answer could help you? I hope so. If not then it just may be a bug.

like image 163
importnumpyasnp Avatar answered Oct 05 '22 22:10

importnumpyasnp


try this code

-(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;
 }
like image 34
Chirag Kalsariya Avatar answered Oct 05 '22 22:10

Chirag Kalsariya