Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3937164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:02:14+00:00 2026-05-20T00:02:14+00:00

My goal is to write a custom camera view controller that: Can take photos

  • 0

My goal is to write a custom camera view controller that:

  1. Can take photos in all four interface orientations with both the back and, when available, front camera.
  2. Properly rotates and scales the preview “video” as well as the full resolution photo.
  3. Allows a (simple) effect to be applied to BOTH the preview “video” and full resolution photo.

Implementation (on iOS 4.2 / Xcode 3.2.5):

Due to requirement (3), I needed to drop down to AVFoundation.

I started with Technical Q&A QA1702 and made these changes:

  1. Changed the sessionPreset to AVCaptureSessionPresetPhoto.
  2. Added an AVCaptureStillImageOutput as an additional output before starting the session.

The issue that I am having is with the performance of processing the preview image (a frame of the preview “video”).

First, I get the UIImage result of imageFromSampleBuffer: on the sample buffer from captureOutput:didOutputSampleBuffer:fromConnection:. Then, I scale and rotate it for the screen using a CGGraphicsContext.

At this point, the frame rate is already under the 15 FPS that is specified in the video output of the session and when I add in the effect, it drops to under or around 10. Quickly the app crashes due to low memory.

I have had some success with dropping the frame rate to 9 FPS on the iPhone 4 and 8 FPS on the iPod Touch (4th gen).

I have also added in some code to “flush” the dispatch queue, but I am not sure how much it is actually helping. Basically, every 8-10 frames, a flag is set that signals captureOutput:didOutputSampleBuffer:fromConnection: to return right away rather than process the frame. The flag is reset after a sync operation on the output dispatch queue finishes.

At this point I don’t even mind the low frame rates, but obviously we can’t ship with the low memory crashes. Anyone have any idea how to take action to prevent the low memory conditions in this case (and/or a better way to “flush” the dispatch queue)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:02:15+00:00Added an answer on May 20, 2026 at 12:02 am

    To prevent the memory issues, simply create an autorelease pool in captureOutput:didOutputSampleBuffer:fromConnection:.

    This makes sense since imageFromSampleBuffer: returns an autoreleased UIImage object. Plus it frees up any autoreleased objects created by image processing code right away.

    // Delegate routine that is called when a sample buffer was written
    - (void)captureOutput:(AVCaptureOutput *)captureOutput 
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
    fromConnection:(AVCaptureConnection *)connection
    { 
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        // Create a UIImage from the sample buffer data
        UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
    
        < Add your code here that uses the image >
    
        [pool release];
    }
    

    My testing has shown that this will run without memory warnings on an iPhone 4 or iPod Touch (4th gen) even if requested FPS is very high (e.g. 60) and image processing is very slow (e.g. 0.5+ secs).

    OLD SOLUTION:

    As Brad pointed out, Apple recommends image processing be on a background thread so as to not interfere with the UI responsiveness. I didn’t notice much lag in this case, but best practices are best practices, so use the above solution with autorelease pool instead of running this on the main dispatch queue / main thread.

    To prevent the memory issues, simply use the main dispatch queue instead of creating a new one.

    This also means that you don’t have to switch to the main thread in captureOutput:didOutputSampleBuffer:fromConnection: when you want to update the UI.

    In setupCaptureSession, change FROM:

    // Configure your output.
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
    [output setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);
    

    TO:

    // we want our dispatch to be on the main thread
    [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my goal is to write a stored proc that can collect all field values
The goal: To create a .NET dll i can reference from inside SQL Server
I am trying to write a custom select statement in Hibernate using the getHibernateTemplate()
Are there any utilities that can examine a set of managed assemblies and tell
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
My Goal I would like to have a main processing thread (non GUI), and
My goal is to maintain a web file server separately from my main ASP.NET
My goal here is to create a very simple template language. At the moment,
My goal is to recognize simple gestures from accelerometers mounted on a sun spot.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.