ObjectiveFlickr - Post multiple photos on Flickr from NSMutable array error - ios

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.

Related

I am trying to upload multiple images to SFTP server but right now I am able to upload only one at a time?

I am using NMSSH to connect to SFTP Server from my ios application. I am able to upload one image at a time to server. But want to upload multiple images at once. How can I achieve this. Please suggest.
NMSSHSession *session = [NMSSHSession connectToHost:#"host"
withUsername:#"user"];
if (session.isConnected) {
if (!session.isAuthorized) {
NSArray *authTypes = [session supportedAuthenticationMethods];
}
NSString *privateKey = [[NSBundle mainBundle] pathForResource:#"privatekey" ofType:nil];
[session authenticateByPublicKey:nil
privateKey:privateKey
andPassword:nil];
if (session.isAuthorized) {
NSLog(#"Authentication succeeded");
}
}
NMSFTP *sftp = [[NMSFTP alloc] initWithSession:session];
[sftp connect];
The requirement is like user will take multiple photos using the camera or image picker and I need to save them locally and once they hit sync button I should send/upload all the images to sftp server.
Right now I am trying this functionality in a demo application so I dont have the images from camera or image picker. For testing I am just using the images in my resource.
Below is the sample:-
UIImage *image1 = [UIImage imageNamed:#"flower.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Wall.jpg"];
UIImage *image3 = [UIImage imageNamed:#"Circle.jpg"];
NSMutableData *data = [[NSMutableData alloc]init];
NSMutableData *data2 = [[NSMutableData alloc]init];
NSMutableData *data3 = [[NSMutableData alloc]init];
[data appendData:UIImagePNGRepresentation(image1)];
[data2 appendData:UIImagePNGRepresentation(image2)];
[data3 appendData:UIImagePNGRepresentation(image3)];
[sftp writeContents:data toFileAtPath:#"serverPath/image1.jpg"];
[sftp writeContents:data2 toFileAtPath:#"serverPath/image2.jpg"];
[sftp writeContents:data3 toFileAtPath:#"serverPath/image3.jpg"];
[session disconnect];

How to generate thumbnails of images on iCloud Drive?

This appears easy, but the lack of documentation makes this question impossible to guess.
I have pictures and videos on my app's icloud drive and I want to create thumbnails of these assets. I am talking about assets on iCloud Drive, not the iCloud photo stream inside the camera roll. I am talking about the real iCloud Drive folder.
Creating thumbnails from videos are "easy" compared to images. You just need 2 weeks to figure out how it works, having in mind the poor documentation Apple wrote but thumbnails from images seem impossible.
What I have now is an array of NSMetadataItems each one describing one item on the iCloud folder.
These are the methods I have tried so far that don't work:
METHOD 1
[fileURL startAccessingSecurityScopedResource];
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
__block NSError *error;
[coordinator coordinateReadingItemAtURL:fileURL
options:NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly
error:&error
byAccessor:^(NSURL *newURL) {
NSDictionary *thumb;
BOOL success = [newURL getResourceValue:&thumb forKey:NSURLThumbnailDictionaryKey error:&error];
UIImage *thumbnail = thumb[NSThumbnail1024x1024SizeKey];
}];
[fileURL stopAccessingSecurityScopedResource];
The results of this method are fantastic. Ready for that? Here we go: success = YES, error = nil and thumbnail = nil.
ANOTHER METHOD
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL
options:nil];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMake(0, 60); // time range in which you want
NSValue *timeValue = [NSValue valueWithCMTime:time];
[imageGenerator generateCGImagesAsynchronouslyForTimes:#[timeValue] completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * error) {
thumbnail = [[UIImage alloc] initWithCGImage:image];
}];
error = The requested URL was not found on this server. and thumbnail = nil
This method appears to be just for videos. I was trying this just in case. Any equivalent of this method to images?
PRIMITIVE METHOD
NSData *tempData = [NSData dataWithContentsOfUrl:tempURL];
NOPE - data = nil
METHOD 4
The fourth possible method would be using ALAsset but this was deprecated on iOS 9.
I think that all these methods fail because they just work (bug or not) if the resource is local. Any ideas on how to download the image so I can get the thumbnail?
Any other ideas?
thanks
EDIT: after several tests I see that Method 1 is the only one that seems to be in the right direction. This method works poorly, sometimes grabbing the icon but most part of the time not working.
Another point is this. Whatever people suggests me, they always say about downloading the whole image to get the thumbnail. I don't think this is the way to go. Just see how getting thumbnails of video work. You don't download the whole video to get its thumbnail.
So this
question remains open.
The Photos-Framework or AssetsLibrary will not work here as you would have to import your iCloud Drive Photos first to the PhotoLibrary to use any methods of these two frameworks.
What you should look at is ImageIO:
Get the content of the iCloud Drive Photo as NSData and then proceed like this:
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)(imageData), NULL );
NSDictionary* thumbOpts = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways,
[NSNumber numberWithInt:160],(id)kCGImageSourceThumbnailMaxPixelSize,
nil];
CGImageRef thumbImageRef = CGImageSourceCreateThumbnailAtIndex(source,0,(__bridge CFDictionaryRef)thumbOpts);
UIImage *thumbnail = [[UIImage alloc] initWithCGImage: thumbImageRef];
After testing several solutions, the one that seems to work better is this one:
[fileURL startAccessingSecurityScopedResource];
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] init];
__block NSError *error;
[coordinator coordinateReadingItemAtURL:fileURL
options:NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly
error:&error
byAccessor:^(NSURL *newURL) {
NSDictionary *thumb;
BOOL success = [newURL getResourceValue:&thumb forKey:NSURLThumbnailDictionaryKey error:&error];
UIImage *thumbnail = thumb[NSThumbnail1024x1024SizeKey];
}];
[fileURL stopAccessingSecurityScopedResource];
This solution is not perfect. It will fail to bring the thumbnails sometimes but I was not able to find any other solution that works 100%. Others are worst than that.
This works for me.It has a little bit different
func genereatePreviewForOnce(at size: CGSize,completionHandler: #escaping (UIImage?) -> Void) {
_ = fileURL.startAccessingSecurityScopedResource()
let fileCoorinator = NSFileCoordinator.init()
fileCoorinator.coordinate(readingItemAt: fileURL, options: .immediatelyAvailableMetadataOnly, error: nil) { (url) in
if let res = try? url.resourceValues(forKeys: [.thumbnailDictionaryKey]),
let dict = res.thumbnailDictionary {
let image = dict[.NSThumbnail1024x1024SizeKey]
completionHandler(image)
} else {
fileURL.removeCachedResourceValue(forKey: .thumbnailDictionaryKey)
completionHandler(nil)
}
fileURL.stopAccessingSecurityScopedResource()
}
}
It looks like you are generating your thumbnail after the fact. If this is your document and you are using UIDocument, override fileAttributesToWriteToURL:forSaveOperation:error: to insert the thumbnail when the document is saved.

For-Loop to quick to present View Controller

i want to add multiple passbook passes by running through a array with URLs. The problem is that the loop counts faster than the view controller can present.
Here s my code:
NSArray *passURLArray = [NSArray new];
passURLArray = response;
for (int i = 0; passURLArray.count; i++) {
NSString *passURLString = [NSString stringWithFormat:#"http://test.de%#", [passURLArray objectAtIndex:i]];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:passURLString]];
NSError *error;
PKPass *pass = [[PKPass alloc] initWithData:data error:&error];
[[UIApplication sharedApplication] openURL:[pass passURL]];
PKAddPassesViewController *passVC = [[PKAddPassesViewController alloc] initWithPass:pass];
passVC.delegate = self;
[passVC setDelegate:(id)self];
[self presentViewController:passVC animated:YES completion:nil];
}
I get this error message:
Attempt to present PKAddPassesViewController: 0xca5f7d0 on
PaymentViewController: 0x14882290 which is waiting for a delayed
presention of PKAddPassesViewController: 0xb169470 to complete
Thanks in advance.
Check if you're on the last iteration of the loop. If you are, animate the display, if not, don't animate it.
That said, it's nasty from a user standpoint. You should probably think about a nicer way of presenting, like showing a list or animating between each display when addPassesViewControllerDidFinish: is called.

Add Image to Facebook Wall

I am trying to implement a previous answer. Here is the code recommended:
-(void) postImageToFB:(UIImage *) image
{
NSData* imageData = UIImageJPEGRepresentation(image, 90);
Facebook* fb = [(uploadPicAppDelegate *)[[UIApplication sharedApplication] delegate] facebook ];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[fb accessToken],#"access_token",
#"message text", #"message",
imageData, #"source",
nil];
[fb requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
But I am getting a error not found on the term: uploadPicAppDelegate
I can't find this in the Facebook sdk or a search of the web (other than additional similar code). Is this a custom created data type --or -- am I missing a file that needs to be imported?
Thanks.
In your params dictionary the value for #"source" should be a link for the picture and not a NSData object.

iOS & Facebook: upload an image along with user comment to the wall

I have an app, it takes screenshot of itself and I would like to post it on user's wall along with a comment.
The problem is, I seemingly tried every option and none of it works:
[facebook dialog:#"feed" ...] doesn't work because you can supply only a URL, not a UIImage.
[facebook requestWithGraphPath ...] doesn't work because the user is not prompted for the comment.
I could first (silently) try to upload the screenshot with requestWithGraphPath and then feed its newly created URL to dialog which doesn't work because of "FBCDN image is not allowed in stream". All of this is of course using callbacks which, including login callback, makes for nice big mess.
I even tried ShareKit but forgot about it after the app strangely could/couldn't login.
Please, how can I share both an image and a comment?
If you are using facebook's latest iOS SDK, in Hackbook sample app, look for APICallsViewController.m file. There is this method that you can post using UIImage directly:
/*
* Graph API: Upload a photo. By default, when using me/photos the photo is uploaded
* to the application album which is automatically created if it does not exist.
*/
- (void) apiGraphUserPhotosPost {
NSString *sourcePath = [[NSBundle mainBundle] resourcePath];
NSString *file1 = [sourcePath stringByAppendingPathComponent:#"/MP1101frame1002.jpg"];
UIImage *img = [[[UIImage alloc]initWithContentsOfFile:file1]autorelease];
[self showActivityIndicator];
currentAPICall = kAPIGraphUserPhotosPost;
HackbookAppDelegate *delegate = (HackbookAppDelegate *) [[UIApplication sharedApplication] delegate];
// Download a sample photo
NSURL *url = [NSURL URLWithString:#"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
// UIImage *img = [[UIImage alloc] initWithData:data]; //here you can insert your UIImage
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
yourMessageHere, #"message", //whatever message goes here
img, #"picture", //img is your UIImage
nil];
[[delegate facebook] requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
If you haven't already, you may want to try the latest development branch of ShareKit at this address:
https://github.com/Sharekit/Sharekit
It has significant improvements added by the community over the canonical version.

Resources