简体   繁体   中英

How to upload images synchronously on Flickr in iOS?

I am working with an application in which i have to upload images on flickr . When i select 15-20 images at once and call uploading methods , it starts uploading and there is no issue with that , but the problem is the when select more than 40 images and starts uploading at once, its memory increases over 300mb and application crashes. I don't know is this the architecture issue of iOS operating system or Flickr api, which starts uploading all the images at once. Is there any way by which i send one image at a time and after uploading it i send second image, so only one image uploaded at once and there will no issue of memory. Some of my Upload method for flickr :

-(void)startupuploader:(NSString *)filePath{

        image=YES;

        NSLog(@"filepath in flicke : %@",filePath);

        JPEGData = [[NSData alloc]initWithContentsOfFile:filePath];

        // NSLog(@"JPEGData in flicke : %@",JPEGData);

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]];

        NSLog(@"data in flicke : %@",data);




        [self.flickrRequest uploadImageStream:[NSInputStream inputStreamWithData:JPEGData] suggestedFilename:@"Flickr"

                                     MIMEType:@"image/jpeg" arguments:[NSDictionary dictionaryWithObjectsAndKeys:@"0", @"is_public", nil]];
  [UIApplication sharedApplication].idleTimerDisabled = YES;

  }

When i call this method for uploading it take all path at once and start uploading all the images at once , which results in crashing my application. Please help me out . Thanks in advance.

A couple of thoughts:

  1. Rather than doing it sequentially, (for which you'll pay a serious performance penalty) I might suggest still doing this concurrently, but controlling the degree of concurrency. For example, I might suggest wrapping upload in NSOperation and add these operations to operation queue with a maxConcurrentOperationCount of 4 or 5. This way you enjoy some concurrency (and the performance gain that will yield), but it constrains it so you don't exhaust limited network connections or use up too much memory at once.

    It's hard for me to get much more specific than that, as I'm unfamiliar with this uploadImageStream method, though. Is that your own method (in which case you should update your question showing us that method) or is that a Flickr-provided method? Usually if methods run asynchronously they provide either a completion block or delegate method to indication completion. You'd need some completion pattern in order to be able to enjoy a controlled degree of concurrency via NSOperationQueue .

  2. Rather than load the file into a NSData and then creating a stream from that, why don't you create a [NSInputStream inputStreamWithFileAtPath:filePath] , and bypass the NSData entirely. That should seriously constrain the memory usage, all by itself. Your current pattern will try to load all of these images completely into memory at the same time. By using streams, it will curtail the amount of memory used at any given time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM