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

The Archive Base Latest Questions

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

I am saving the images when there is internet connection in my iphone. Now

  • 0

I am saving the images when there is internet connection in my iphone.
Now i want to load the images which are saved in documents directory. I am getting the path, but the image is not displaying. Please help me in this.
Thanks in advance.

Here is my code:

+(UIImage *) getImagewithName:(NSString*) imagepath
{
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    UIImage *gimage=[UIImage imageWithContentsOfFile: [NSString stringWithFormat:@"%@/%@",docDir,imagepath]];

    NSLog(@"%@/%@",docDir,imagepath);

    return gimage;
}

+(void)DownloadImage:(NSString*)ImagePath {
    if ([ImagePath isEqualToString:@""])
       return;

    UIImage *dimage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:/uploads/%@",ImagePath]]]];
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"pathIMAGEPATHS:::::::%@",ImagePath);
    NSLog(@"path:::::::%@",docDir);
    NSArray *patharr=[ImagePath componentsSeparatedByString:@"."];
    NSString* Ext=[NSString stringWithFormat: @"%@",[patharr objectAtIndex:1]];
    NSString *FilePath = [NSString stringWithFormat: [NSString stringWithFormat:@"%@/%@",docDir,ImagePath]];
    if([Ext isEqualToString:@"png"])
    {
        NSLog(@"saving png");

        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(dimage)];
        [data1 writeToFile:FilePath atomically:YES];
    }
    else if([Ext isEqualToString:@"jpeg"])
    {
        NSLog(@"saving jpeg");
        //NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
        NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(dimage, 1.0f)];//1.0f = 100% quality
        [data2 writeToFile:FilePath atomically:YES];
    }
    else 
    {
        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(dimage)];
        [data1 writeToFile:FilePath atomically:YES];

    }
    NSLog(@"saving image done");

    [dimage release];

}
  • 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:54:01+00:00Added an answer on May 20, 2026 at 12:54 am

    viggio24 makes a good point. But moreover, this code seems incredibly complicated to do something incredibly simple. Why do you download to data, then turn it into an image, just to turn it back into data, and finally write it? And then you read it again and make a new image? Why not just download the file to disk and read it? UIImage will do all the work of managing PNG and JPEG for you. Something like:

    +(void)downloadImage:(NSString*)imagePath {
        if ([imagePath isEqualToString:@""])
            return;
    
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[@"http://110.234.132.132/MBCMcAdmin/uploads" stringByAppendingPathComponent:imagePath]]];
        NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        [data writeToFile:[docDir stringByAppendingPathComponent:imagePath] atomically:YES];
    }
    

    This is still a horrible idea in most cases, though, since it blocks the calling thread. It’s better to do this with NSURLDownload which will do it all for you in the background and tell you when it’s done.

    I’d simplify the code, and then follow viggio24’s comments, making sure that you error check this stuff. You’re probably just not reading the file correctly or failing to write it.

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

Sidebar

Related Questions

Which one is better at runtime: 1)saving images of size 250X120 in sqlite. 2)Saving
So I'm saving small images to core data which take a really short amount
I am using camera functionality in my application and saving the images in documents
I know there are some solutions out there for saving and loading images on
Is there a way to prevent users from saving images when they do a
I m saving path of images, audio and video files that path is of
I am getting H.264 images from an IP camera and want to save the
I'm working on richtextbox, which can handle images. I'm saving xaml content of richtextbox
I am saving images to the photo library and would like to retrieve them
I am saving files (images, Excel doc, Word doc, exe files, bat files and

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.