0

I am capturing an image using AVFoundation through the camera and then displaying it through a segue to another viewController which has a UIImageView. The image size is 1936 x 2592 and it takes 5.5 seconds for the screen to update with the image after viewWillAppear gets called.

Is this normal and due to simply the size of the image? This is on an iPhone4 running iOS6. This delay is not acceptable so I have to find a solution, probably trying to scale down the image first, but I thought I would ask before jumping through hoops.

Update

Reducing image size to 484 x 648 takes even longer: 5.6 seconds. There was a request in the comments for the capture code I am using. It is at the bottom of this post. Also, I am only capturing the one image and then displaying it.

// Console output:

have image

2013-03-03 11:37:16.741 RPFaceCamera[4867:907] viewWillAppear - time taken: 0.000215>

2013-03-03 11:37:22.249 RPFaceCamera[4867:907] viewDidAppear - time taken: 5.507458>


//Code

- (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]);
}

//capture code - (I think I cribbed this from Apple's AVDemo). Objective-C's format leaves a lot to be desired for readability.

- (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];
              }                                                      
          }
     ];
}

// method called on main thread when capture complete - (void)showFaceView { NSLog(@"showfaceView");

    [self destroyAVCapture];
    [self.delegate dismissCameraWithImage:self.faceImage];
}
spring
  • 18,009
  • 15
  • 80
  • 160
  • How long does it take to load smaller images? Say 800 X 600 or the like, if it's taking awhile for those too you may want to clear out the cache/files you dont need on your phone to speed it up. If it doesn't take long for small images but takes awhile for that one, I'd imagine it's just because of the image size. – Him_Jalpert Mar 03 '13 at 16:53
  • Just tried reducing to 484 x 648 and it takes even longer: 5.6 seconds. Really odd. There is 4.7 gb available on the device. – spring Mar 03 '13 at 16:56
  • How did you reduce the size? Did you actually make it smaller in an image editor? – dgund Mar 03 '13 at 17:02
  • Use instruments to find out what exactly takes so long. – Sven Mar 03 '13 at 17:04
  • @DGund - I set the scale in [UIImage imageWithCIImage:ciImage scale:4.0f orientation:[orientation intValue] since I am capturing the image from the camera using AVFoundation. – spring Mar 03 '13 at 17:08
  • Your going to need to post some relevant code. From where you actually capture and save the image. – Mick MacCallum Mar 03 '13 at 17:11
  • What image format do you use? JPEG usually takes a long time to decode.. that's (among quality) is one of the reasons apple suggests to use PNG for all resources – Martin Ullrich Mar 03 '13 at 17:16
  • To be clear, with AVFoundation, are you capturing one picture at a time or do you have a capture session running (video) and you are using the captureoutput delegate to get an image? – Khaled Barazi Mar 03 '13 at 19:41
  • 1
    I agree with @Sven. Don't ask _us_ what's taking so long; ask Instruments. That's what it's for! Otherwise you're just wasting your time guessing. – matt Mar 03 '13 at 20:20

1 Answers1

0

I'd conjecture that physically making the image smaller in an image editor would result in smaller load times.

Right now, per your comment, you are setting the scale with [UIImage imageWithCIImage:ciImage scale:4.0f orientation:[orientation intValue]

This doesn't actually make the image file any smaller, and thus doesn't make the load times smaller. Check out the answers from this Stackoverflow post for information on reducing the UIImage size with NSData. This post discusses programmatically resizing a UIImage. Perhaps they will work better.

Community
  • 1
  • 1
dgund
  • 3,459
  • 4
  • 39
  • 64