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