我有一些代码正在处理我的某个应用程序;所以我复制了代码,进行了必要的更改(即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];
}
UPDATE:在捕获图像的某个时刻,我收到此警告:
快照未呈现的视图会导致空快照。确保在屏幕更新后快照或快照之前,您的视图至少呈现过一次。
也许这与它有关?我试图纠正警告,无济于事。
我在Google和SO中寻找类似的问题,但一无所获。我已经为此工作了两天,并决定是时候寻求帮助了。提前谢谢。
SD
答案 0 :(得分:2)
试试这段代码
-(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;
}
答案 1 :(得分:2)
我也遇到过这个问题。经过几个小时的研究,我发现了这个问题:iOS8 Snapshotting a view that has not been rendered results in an empty snapshot
也许最后的答案可以帮到你?希望如此。如果没有,那么它可能只是一个错误。
答案 2 :(得分:1)
它对我有用。也许从故事板中删除oBookImage对viewcontroller的引用,然后再重新绑定它。