【问题标题】:Why is it taking 5.5 seconds to display an image (in UIImageVIew)?为什么显示图像需要 5.5 秒(在 UIImageVIew 中)?
【发布时间】:2013-03-03 16:48:48
【问题描述】:

我正在通过相机使用 AVFoundation 捕获图像,然后通过 segue 将其显示到另一个具有 UIImageView 的视图控制器。图像大小为 1936 x 2592,调用viewWillAppear 后,屏幕需要 5.5 秒才能更新图像。

这是正常的,仅仅是因为图片的大小吗?这是在运行 iOS6 的 iPhone4 上。这种延迟是不可接受的,所以我必须找到解决方案,可能先尝试缩小图像,但我想我会在跳过箍之前先问一下。

更新

将图像尺寸缩小到 484 x 648 需要更长的时间:5.6 秒。 cmets 中有我正在使用的捕获代码的请求。它在这篇文章的底部。另外,我只捕获一张图像然后显示它。

// 控制台输出:

有图片

2013-03-03 11:37:16.741 RPFaceCamera[4867:907] viewWillAppear - 耗时:0.000215>

2013-03-03 11:37:22.249 RPFaceCamera[4867:907] viewDidAppear - 耗时:5.507458>


//代码

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.faceImage != nil){
        start = [NSDate date];
        printf("have image     \n");
        self.faceImageView.image = self.faceImage;
        NSLog(@"viewWillAppear - time taken: %f", -[start timeIntervalSinceNow]);
    }

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear - time taken: %f", -[start timeIntervalSinceNow]);
}

//捕获代码 - (我想我是从 Apple 的 AVDemo 中抄来的)。 Objective-C 的格式在可读性方面还有很多不足之处。

- (void)captureImage
{
    AVCaptureConnection *connection;
    connection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo];

    [stillImageOutput setOutputSettings:[NSDictionary
                                         dictionaryWithObject:[NSNumber numberWithInt:kCMPixelFormat_32BGRA]
                                         forKey:(id)kCVPixelBufferPixelFormatTypeKey]];

    [stillImageOutput captureStillImageAsynchronouslyFromConnection:connection
          completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
              if(error)
                  NSLog(@"captureImage failed");
              else{

                  CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);
                  CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault,
                                                                              imageDataSampleBuffer,
                                                                              kCMAttachmentMode_ShouldPropagate);

                  CIImage *ciImage =
                  [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer
                                                options:(__bridge NSDictionary *)attachments];

                  NSNumber *orientation = (__bridge NSNumber *)(CMGetAttachment(imageDataSampleBuffer,
                                                      kCGImagePropertyOrientation, NULL));

                  printf("original orientation     %d\n", [orientation intValue]);

                  self.faceImage = [UIImage imageWithCIImage:ciImage scale:1.0f orientation:[orientation intValue]];
                  printf("self.faceImage     %d\n", self.faceImage.imageOrientation);
                  printf("self.faceImage    width: %f height: %f\n",  self.faceImage.size.width, self.faceImage.size.height);

                  __weak __typeof__(self) weakSelf = self;
                  [weakSelf performSelectorOnMainThread:@selector(showFaceView) withObject:nil waitUntilDone:NO];
              }                                                      
          }
     ];
}

// 捕获完成时在主线程上调用的方法 - (void)showFaceView { NSLog(@"showfaceView");

    [self destroyAVCapture];
    [self.delegate dismissCameraWithImage:self.faceImage];
}

【问题讨论】:

  • 加载较小的图像需要多长时间?比如说 800 X 600 等,如果它们也需要一段时间,您可能需要清除手机上不需要的缓存/文件以加快速度。如果小图像不需要很长时间,但需要一段时间,我想这只是因为图像大小。
  • 刚刚尝试减少到​​ 484 x 648 并且需要更长的时间:5.6 秒。真的很奇怪。设备上有 4.7 GB 可用空间。
  • 你是如何缩小尺寸的?你真的在图像编辑器中把它变小了吗?
  • 使用工具找出究竟需要这么长时间。
  • 我同意@Sven。不要问我们什么花了这么长时间;问仪器。这就是它的目的!否则你只是在浪费时间猜测。

标签: ios ios6 uiimageview


【解决方案1】:

我猜想在图像编辑器中将图像缩小会导致加载时间更短。

现在,根据您的评论,您正在使用[UIImage imageWithCIImage:ciImage scale:4.0f orientation:[orientation intValue] 设置比例

这实际上不会使图像文件更小,因此不会使加载时间更短。 查看this Stackoverflow 帖子的答案,了解有关使用NSData 减小UIImage 大小的信息。 This 帖子讨论了以编程方式调整 UIImage 的大小。也许他们会更好地工作。

【讨论】:

  • OP 正在从相机捕捉图像。
  • @duci9y 一旦[从相机]获得UIImage,就可以通过编程方式应用它
  • 我指的是你建议在图像编辑器中缩小图像。
  • @duci9y 您就在那儿,但在发布此答案之前我确实意识到了这一点;不用担心
猜你喜欢
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 2013-03-21
  • 2019-04-11
  • 2017-06-22
  • 1970-01-01
相关资源
最近更新 更多