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 8696799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:21:16+00:00 2026-06-13T01:21:16+00:00

So I have been at it all day to no luck and it has

  • 0

So I have been at it all day to no luck and it has been needless to say quite frustrating, I have looked up many examples and downloadable categories which all tout being able to crop images flawlessly. Which they do, However the minute i try to do it from an image genrated via AVCaptureSession it does not work as well. I consulted both these sources

http://codefuel.wordpress.com/2011/04/22/image-cropping-from-a-uiscrollview/

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

and the project from the first link seems to work directly as advertised but as soon as i hack it to do the same magic on an av capture image…nope…

does anyone have insight into this? Also here is my code for reference.

- (IBAction)TakePhotoPressed:(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;
     }
     }
     if (videoConnection) { break; }
     }

     //NSLog(@"about to request a capture from: %@", stillImageOutput);
     [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
         if (exifAttachments)
         {
             // Do something with the attachments.
             //NSLog(@"attachements: %@", exifAttachments);
         }
         else

         NSLog(@"no attachments");

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
         UIImage *image = [[UIImage alloc] initWithData:imageData];

         NSLog(@"%f",image.size.width);
         NSLog(@"%f",image.size.height);


         float scale = 1.0f/_scrollView.zoomScale;

         CGRect visibleRect;
         visibleRect.origin.x = _scrollView.contentOffset.x * scale;
         visibleRect.origin.y = _scrollView.contentOffset.x * scale;
         visibleRect.size.width = _scrollView.bounds.size.width * scale;
         visibleRect.size.height = _scrollView.bounds.size.height * scale;

         UIImage* cropped = [self cropImage:image withRect:visibleRect];

         [croppedImage setImage:cropped];         

         [image release];
     }
      ];

    [croppedImage setHidden:NO];


}

cropImage function used above.

-(UIImage*)cropImage :(UIImage*)originalImage withRect :(CGRect) rect
{


    CGRect transformedRect=rect;
    if(originalImage.imageOrientation==UIImageOrientationRight) 
    {
        transformedRect.origin.x = rect.origin.y;
        transformedRect.origin.y = originalImage.size.width-(rect.origin.x+rect.size.width);
        transformedRect.size.width = rect.size.height;
        transformedRect.size.height = rect.size.width;
    }

    CGImageRef cr = CGImageCreateWithImageInRect(originalImage.CGImage, transformedRect);
    UIImage* cropped = [UIImage imageWithCGImage:cr scale:originalImage.scale orientation:originalImage.imageOrientation];
    [croppedImage setFrame:CGRectMake(croppedImage.frame.origin.x, 
                                      croppedImage.frame.origin.y, 
                                      cropped.size.width, 
                                      cropped.size.height)];

    CGImageRelease(cr);
    return cropped;
}

I am also tempted for verbosity and arming whomever might help me in my plight with as much information as possible to post my init of my scrollView and avcapture session. However That may be a bit too much so if you want to see it just ask.

Now as for results of what the code actually does?..

What it looks like before i take the picture

before shot

And After…

after shot

EDIT:

Well I have a few views now and no comment’s so either no one has figured it out or it’s so simple they thought i would have figured it out again…In any case i have not made any progress. So for anyone interested here is a small sample app with the code all set up and you can see what i am doing

https://docs.google.com/open?id=0Bxr4V3a9QFM_NnoxMkhzZTVNVEE

  • 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-06-13T01:21:17+00:00Added an answer on June 13, 2026 at 1:21 am

    It seems that this little conundrum did not only have me stumped as after nearly a week,but a scant few of whoever viewed my question had no suggestions either. I must say for this particular problem i could not get it to work in this way, I pondered and tinkered and mused for a while to no avail. Until i did this

    [self HideElements];
    
    UIGraphicsBeginImageContext(chosenPhotoView.frame.size);
    [chosenPhotoView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self ShowElements];
    

    And that’s it, less code and it worked pretty much instantly. So instead of trying to crop an image via the scrollview I take a screenshot of the screen at that time then crop the image using the scrollviews frame variables. And the hide/show element functions hide any overlapping elements on the picture i want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying all day to get app-engine sdk to work nothing has
I have been wrangling with my code all day with no luck in solving
I have been working on this all day with no luck. Its night now
I have been stumped all day on this problem. Basically I want to check
I have been trying all day to get some data properly formatted in a
I have been struggling with this all day. I have an html table in
I have been tinkering with javasript (jquery) all day. I want to create a
Good day everyone. I have been having the same problem all day at work
I'm stuck on this and have been all day.. I'm still pretty new to
I have been trying all day to "catch" or assign/bind to the chosen state

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.