5

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

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • shouldn't oBookImage.image = newImage; be self.oBookImage.image = newImage; – Ro4ch May 14 '16 at 15:53
  • @Ro4ch sorry, didn't help... I don't have it that way in the working app... thanks anyway... – SpokaneDude May 14 '16 at 15:56
  • 1
    Sorry, what is the problem did you get? – Tien May 14 '16 at 16:37
  • Is this `if ([mediaType isEqualToString:(NSString *)kUTTypeImage])` true? – J. Lopes May 14 '16 at 16:48
  • @J.Lopes yes, it is... everything that is supposed to be executed, is; and the last statement where I move the *newImage* to the UIImageView is also executed because when I look at *oBookImage.image* the image appears to be there for a fraction of a second (and in the debugger), but when the method completes, it's gone! – SpokaneDude May 14 '16 at 17:10
  • Have you tried [self.oBookImage setImage:newImage]; ? – Ro4ch May 18 '16 at 07:25
  • 1
    do you have this project on github? – Ro4ch May 18 '16 at 18:58
  • Ok, what if you wrapped the code in a block? maybe something is executing after you set the image that makes the image return nil. Wrap it in a block and the image will still be processed will the system continues. – Ro4ch May 19 '16 at 14:37
  • @Ro4ch It doesn't get set to nil; it (the image) shows until a piece of *phantom* code is hit... also, did you read the UPDATE? I think this might have something to do with it, but as I indicated, I was not able to resolve/remove the warning. – SpokaneDude May 19 '16 at 15:24
  • On the render issue, try wrapping your imagepicker code with this, dispatch_async(dispatch_get_main_queue(), ^{ }); - like this guy http://stackoverflow.com/a/30008682/5716449 Hope it does something good. – Ro4ch May 19 '16 at 18:50
  • @Ro4ch no, unfortunately, nothing changed: still get the warning and the image doesn't stay in the UIImageView... big bummer! – SpokaneDude May 19 '16 at 19:21
  • O_O alright, I will see if I can think of something else. – Ro4ch May 19 '16 at 19:54
  • Maybe try putting everything (i.e. reading info dict, constructing image, reading back image from context) inside the completion block to `dismissViewController:completion:`? – fullofsquirrels May 23 '16 at 16:35

3 Answers3

2

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

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.

Community
  • 1
  • 1
importnumpyasnp
  • 262
  • 5
  • 14
  • I also believe it's a bug, but am going to try Apple Tech Support and see what they say. Thank you all for your suggestions. SD – SpokaneDude May 24 '16 at 15:02
1

It works for me. Perhaps delete the reference of oBookImage to your viewcontroller from storyboard, then re-bind it again.

Jason Silver
  • 706
  • 7
  • 10