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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:55:53+00:00 2026-05-29T04:55:53+00:00

This is written in my viewDidLoad method NSOperationQueue *queue = [NSOperationQueue new]; NSInvocationOperation *op

  • 0

This is written in my viewDidLoad method

 NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *op = [[NSInvocationOperation alloc] 
                                        initWithTarget:self
                                        selector:@selector(downloadImage) 
                                        object:nil];
    [queue addOperation:op]; 

// The the rest in other methods;

- (void)downloadImage{
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:person.picURL]];
    UIImage* image = [[UIImage alloc] initWithData:imageData] ;


    [self showImage: image];
}

I have tried adding the following codes above [self showImage: image]; and i ended up with an exception.

1.) [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:NO];

2.) [self performSelectorInBackground:@selector(showImage:) withObject:image];

// This is the showImage code.

- (void)showImage:(UIImage *)img {

    if (img != nil) 
    {    
        myImageView = [[UIImageView alloc] initWithImage:img];

        myImageView.frame = CGRectMake(20, 20, 132, 124);

        [scrollView addSubview:myImageView];

        [pictureOfPerson setImage:img];        
    }
}

I am trying to Asynchronously download the image and cache it. The image gets downloaded Asynchronously but i am not sure if it gets cached.

1.) How to cache the image and use it in when the view loads again

2.) While the image is downloading, what if i click on another view. Then i need to stop the download. How can i write this code. I know that i have to write it in the viewDidDissapear method.

3.) Is my code correct. Have i missed anything or is there a better approach to do this? if so tutorial or some sample code please

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

    I’d use GCD to download the image. It’s much simpler to use than NSOperation. Here’s an example:

    UIImage *personPicture;
    personPicture = [self.imageCache objectForKey:person.picURL];
    if (!personPicture) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:person.picURL]];
            personPicture = [UIImage imageWithData:imageData];
            [self.imageCache setObject:imageData forKey:person.picURL];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showImage:personPicture];
            });
        });
    }
    else {
        [self showImage:personPicture];
    }
    

    You could use a class property like an NSMutableDictionary to store the UIImage data and associate it with your URL. If it’s present, use it, if not, download the image.

    -EDIT-
    Adding the code to handle caching.

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

Sidebar

Related Questions

In my viewDidLoad, I have written this code: UIView *footerView = [[[UIView alloc] initWithFrame:CGRectZero]
I have written this clone method for when the parent of the Employee class
yeah! i have written the code inside viewDidLoad method but work properly!!! my prolblem
I've written this method which should check for file changes. public static void watch()
This is written in PHP, but it's really language agnostic. try { try {
I've written this very simple function to replace a file extension using LINQ in
I've written this to try and log onto a forum (phpBB3). import urllib2, re
I've written this sample code to print the length of x. But for some
I've written this piece of code where I've assigned an unsigned integer to two
I've written this code to try and send a url as a post value

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.