如何在 Objective-C 中更改 ImageView 的持续时间
How to change duration of ImageView in Objective-C
我正在为照相亭创建一个简单的相机应用程序。下面的代码是整个项目的示例,我使用 AVFoundation 因为我需要自己的自定义要求。
当用户按下按钮时,它会拍照并立即在 imageView 中显示预览。这是我的第一个主要编码项目,并且是 Objective-C 的新手。有没有办法向 imageView 添加计时器或持续时间,以便它从中删除图像,或者有办法在一段时间后循环到第二张黑色图像,为下一个用户拍照做好准备?
非常感谢您提供的任何帮助!
- (IBAction)takephoto:(id)sender {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in StillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts ]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
}
[StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
}
}];
}
您可以使用 dispatch_after
或 NSTimer
来做到这一点。
dispatch_after:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// do something
});
NSTimer:
[NSTimer scheduledTimerWithTimeInterval:<#delayInSeconds#> target:self selector:@selector(action) userInfo:nil repeats:NO];
- (void)action {
// do something
}
NSTimer
允许您根据需要重复操作,但如果您只需要交换图像,dispatch_after
更容易,因为您可以在范围内进行。
[StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
imageView.image = imageToSwap;
}
}
}];
我正在为照相亭创建一个简单的相机应用程序。下面的代码是整个项目的示例,我使用 AVFoundation 因为我需要自己的自定义要求。
当用户按下按钮时,它会拍照并立即在 imageView 中显示预览。这是我的第一个主要编码项目,并且是 Objective-C 的新手。有没有办法向 imageView 添加计时器或持续时间,以便它从中删除图像,或者有办法在一段时间后循环到第二张黑色图像,为下一个用户拍照做好准备?
非常感谢您提供的任何帮助!
- (IBAction)takephoto:(id)sender {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in StillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts ]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
}
[StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
}
}];
}
您可以使用 dispatch_after
或 NSTimer
来做到这一点。
dispatch_after:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// do something
});
NSTimer:
[NSTimer scheduledTimerWithTimeInterval:<#delayInSeconds#> target:self selector:@selector(action) userInfo:nil repeats:NO];
- (void)action {
// do something
}
NSTimer
允许您根据需要重复操作,但如果您只需要交换图像,dispatch_after
更容易,因为您可以在范围内进行。
[StillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
imageView.image = image;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
imageView.image = imageToSwap;
}
}
}];