我正在尝试编写应用程序,使主对象每秒刷新到另一个。不幸的是,尝试不在第二个UIImageView上显示任何内容。源代码:
ViewController.h
#import <UIKit/UIKit.h>
@import AVFoundation;
@interface ViewController : UIViewController {
IBOutlet UIView *vImagePreview;
}
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
@property(nonatomic, retain) IBOutlet UIImageView *imagePreview;
@property(nonatomic, retain) IBOutlet UIImageView *takePreview;
-(IBAction)scanImage: (UIButton *)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize vImagePreview;
- (void)viewDidLoad {
[super viewDidLoad];
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = self.vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];
[session startRunning];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)scanImage:(UIButton *)sender {
int success = 0;
while ( success < 5 )
{
self.takePreview.image = self.imagePreview.layer;
NSLog (@"OK");
sleep(1);
success++;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我期待着提供帮助。