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

  • Home
  • SEARCH
  • 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 4252332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:46:51+00:00 2026-05-21T04:46:51+00:00

I have a simple iPhone app that allows the user to upload images to

  • 0

I have a simple iPhone app that allows the user to upload images to a server. The problem is, what if they upload a large image file. I want to limit it down to (a max of) 200 KB. I started something but it seems to crash in my while statement.

Here’s the code:

NSString *jpgPath = [NSString stringWithFormat:@"Documents/%@",sqlImageUploadPathTwo];
NSString *jpgPathTwo = [NSString stringWithFormat:@"./../Documents/%@",sqlImageUploadPathTwo];
NSString *yourPath = [NSHomeDirectory() stringByAppendingPathComponent:jpgPath];

NSLog(@"yourPath: %@", yourPath);

NSFileManager *man = [[NSFileManager alloc] init];
NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
int *result = [attrs fileSize];
NSLog(@"Here's the original size: %d", result);

NSLog(@"jpgPath: %@ // jpgPathTwo: %@", jpgPath, jpgPathTwo);

while (result > 9715) {
    UIImage *tempImage = [UIImage imageNamed: jpgPath];
    NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(tempImage, 0.9)];
    [imageData writeToFile:jpgPathTwo atomically:YES];
    NSLog(@"just shrunk it once.");
}

NSLog(@"SIZE AFTER SHRINK: %@", result);

Thanks!
Coulton

  • 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-21T04:46:52+00:00Added an answer on May 21, 2026 at 4:46 am

    Something like this:
    (also note that you declared result as int* (i.e. a pointer), instead of a number, and the condition should be >, not < (otherwise for large files it won’t change them at all). And an extra counter condition is useful to avoid endless loop (basically do it 5 times and then stop doing it, regardless of the size).

    NSFileManager *man = [[NSFileManager alloc] init];
    NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
    int result = [attrs fileSize];
    int count = 0;
    while (result > 9715 && count < 5) {
        UIImage *tempImage = [UIImage imageNamed: jpgPath];
        NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(tempImage, 0.9)];
        [imageData writeToFile:jpgPathTwo atomically:YES];
        NSDictionary *attrs = [man attributesOfItemAtPath: jpgPathTwo error: NULL];
        result = [attrs fileSize];
        count++;
        NSLog(@"just shrunk it once.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple iphone app that's based on the CrashLanding sample app. So
I'm working on a simple iPhone app. I have 1 UINavigationController and 2 subclasses
I have very simple iPhone app, which uses just UIButtons , UIlabels , UITableView
I have a native iPhone app that just loads a website using UIWebView .
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple 2-column layout with a footer that clears both the right
(This algorithm is for an iPhone app I am working on, if that helps
I am developing an iPhone application that allows to basically click through a series
My fellow developer and I are creating an iPhone app that is creating a

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.