Upload multiple photos with ObjectiveFlickr

119 views
Skip to first unread message

Granit Gjevukaj

unread,
Nov 18, 2013, 4:23:28 PM11/18/13
to objecti...@googlegroups.com
Dear All,

I am making an app that lets users post to their Flickr photo streams. I am using ObjectiveFlickr to handle the authorization and posting process.

For posting a single picture i am using the following code:

if (isSinglePic) {
UIImage *img = self.singlePic;
NSData *JPEGData = UIImageJPEGRepresentation(img, 1.0);

self.flickrRequest.sessionInfo = __kUploadImageStep;
[self.flickrRequest uploadImageStream:[NSInputStream inputStreamWithData:JPEGData] suggestedFilename:self.descriptionTextField.text MIMEType:@"image/jpeg" arguments:[NSDictionary dictionaryWithObjectsAndKeys:@"0", @"is_public", nil]];

//[NSDictionary dictionaryWithObjectsAndKeys:photoID, @"photo_id", @"PicknPost", @"title", @"via PicknPost", @"description", nil]]

[UIApplication sharedApplication].idleTimerDisabled = YES;
}

This works just fine. However, when i try to post multiple pics that i stored in a NSMutableArray, ObjectiveFlickr uploads only the first picture.

My code for posting multiple photos looks like this:

NSMutableArray *array = [[NSMutableArray alloc] initWithArray:picsToPost copyItems:YES];


for (UIImage *imgToPost in array) {
NSData *JPEGData = UIImageJPEGRepresentation(imgToPost, 1.0);

self.flickrRequest.sessionInfo = __kUploadImageStep;
[self.flickrRequest uploadImageStream:[NSInputStream inputStreamWithData:JPEGData] suggestedFilename:self.descriptionTextField.text MIMEType:@"image/jpeg" arguments:[NSDictionary dictionaryWithObjectsAndKeys:@"0", @"is_public", nil]];

[UIApplication sharedApplication].idleTimerDisabled = YES;

Does anyone have an idea why the for-in statement doesn't work?

Any help would be appreciated.

lukhnos

unread,
Nov 18, 2013, 4:34:28 PM11/18/13
to objecti...@googlegroups.com

The request object only handles one upload task at a time, and so it does nothing if you call upload many times on the same object if it hasn't finished.

A common pattern is make your image array as an ivar or a property. When you want to upload, grab the first element in the array, call the upload. Once the upload finishes, remove the first element in your delegate method and call upload again. This effectively creates a upload queue. Your upload method can also check first if the request is running; if so, do nothing. This also makes it possible to call the upload method anytime anywhere.

Lukhnos

Granit Gjevukaj

unread,
Nov 18, 2013, 6:45:45 PM11/18/13
to objecti...@googlegroups.com
Dear Lukhnos,

Thank you for your quick reply.

I tried to implement a solution based on your answer. The weird thing is that 
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary is not being called at all, since i'm getting no response whatsoever.

Do you know any example which i could follow in order to fix this problem. I'm new to iOS development and maybe i'm mixing something up in the code.

Thank you very much! 

lukhnos

unread,
Nov 18, 2013, 7:14:44 PM11/18/13
to objecti...@googlegroups.com

Two things to check:

1. Make sure you've set the delegate (e.g. flickrRequest.delegate = self in your calling code) -- this is a common oversight that everyone has from time to time :)
2. Make sure you've also checked your failure handler (flickrAPIRequest:didFailWithError:) -- errors do happen so make sure you also do something about it

Lukhnos

Granit Gjevukaj

unread,
Nov 19, 2013, 1:25:18 PM11/19/13
to objecti...@googlegroups.com
Hi Lukhnos,

I was able to solve the problem based on your suggestions. Thank you for your help :)

Granit

Prerana Polekar

unread,
May 8, 2014, 8:18:25 AM5/8/14
to objecti...@googlegroups.com
Hi,
I am using Objectiveflickr to upload photos from Mac to Flickr. I am facing the same problem as @Granit but only its on Mac. I tried implemented @Lukhnos way of checking whether requested is running or not but I guess correct me if I am wrong because of asynchronous nature of API the control is coming out of for loop while traversing the second image in my NSMutableArray which is making only the first image in the array to get uploaded.

Regarding creating a queue I am not getting the clear idea on how to do that.
Following is my code:-

//Array created with path to two images

NSMutableArray * uplist=[[NSMutableArray alloc]initWithObjects:@"/Users/betteradmin/Pictures/photos/3d_photoshop_nature_landscape_15285.png",@"/Users/betteradmin/Pictures/photos/images.jpeg", nil];


//Delegate method

-(void)MyUpload:(NSMutableArray *)arr withindex:(int)i;
{

NSLog(@"Name=%@",[arr objectAtIndex:(i)]);
NSString *someFilename = @"Foo.png";
NSString *someTitle = @"My PreUploadMac!";
NSString *someDesc = @"^^ :)";
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
someTitle, @"title",
someDesc, @"description",
nil];
if([_flickrRequest isRunning])
{
//Do nothing
NSLog(@"Request is running");
}
else
{
[_flickrRequest uploadImageStream:[NSInputStream inputStreamWithFileAtPath:[arr objectAtIndex:(i)]] suggestedFilename:someFilename MIMEType:@"image/png" arguments:params];
}



}

//Calling the method first time

[self MyUpload:uplist withindex:0];

//Calling the method again after first upload is completed

- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary
{
NSLog(@"%s, return: %@", __PRETTY_FUNCTION__, inResponseDictionary);

[_progressIndicator stopAnimation:self];
[_progressLabel setStringValue:@"API call succeeded"];

for(int ii=1;ii<[uplist count];ii++)
{
[self MyUpload:uplist withindex:ii];
}
}

Basically I am logically and conceptually stuck :( I am new to Objective C Mac so any help would be highly appreciated!!
Thanks a ton in advance.

Prerana

lukhnos

unread,
May 12, 2014, 1:16:18 PM5/12/14
to objecti...@googlegroups.com
Hi,

The way to do it might be easier than you thought. When designing your upload method (as your MyUpload did):

1. If the mutable array is empty, there's nothing to upload. Just return.
2. If the upload request is running, don't run. Return.
3. Otherwise, pick the first element of the array, make the upload request.

Then, in your callback (the upload delegate method):

1. Remove the first element from the mutable array.
2. Call MyUpload again (*).

This requires you to make the mutable array something that both your upload method and upload callback share. You can do this by making the array a property / instance variable, or by passing it as part of the request's context info.

Lukhnos

(*) You'll also need to figure out what to do when there's an error, but once you figure out why this basic setup works and why it's correct, you should be able to extend it.

On Monday, November 18, 2013 1:23:28 PM UTC-8, Granit Gjevukaj wrote:

Prerana Polekar

unread,
May 13, 2014, 1:00:22 AM5/13/14
to objecti...@googlegroups.com
Hi Lukhnos,
Thanks for replying. Using your previous comments in the thread I tried to implement the queue like pattern for upload and after working around it a bit I was successfully able to do so .:):) A complete newbie in Objective-C that is why was having conceptual issues. Kind of getting a hang over it and enjoying using objectiveflickr :)
Thanks a ton again..:) 
Reply all
Reply to author
Forward
0 new messages