我有一些与我的应用程序相关的代码;所以我复制了这些代码,并进行了必要的更改(例如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
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
是真的吗? - J. Lopes