Check what size photos are available from Flickr - objective-c

I'm using objectiveflickr to pull a list of images from a flickr.photos.search query.
I have it working where I pull in the thumbnail and the large size image, however sometimes an image doesn't have a large size available and so in the app it displays flickr's image unavailable image. I checked what is returned through the flickr API and the URL's are regular flickr URL's, so there's no way to tell if an image will or will not have a large image that way.
The only way I can think of doing this is to create another objective flickr delegate and have it do a flickr.photos.getSizes API call and check against each photo.
Anyone have any other suggestions?
Here's my code:
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary
{
int requestedPhotoTotal = [[[inResponseDictionary objectForKey:#"photos"] valueForKey:#"perpage"] intValue];
for (int i=0; i < requestedPhotoTotal; i++)
{
NSDictionary *photoDict = [[inResponseDictionary valueForKeyPath:#"photos.photo"] objectAtIndex:i];
NSString *title = [photoDict valueForKey:#"title"];
NSURL *photoURL = [context photoSourceURLFromDictionary:photoDict size:OFFlickrLargeSize];
NSURL *thumbURL = [context photoSourceURLFromDictionary:photoDict size:OFFlickrThumbnailSize];
NSURL *photoSourcePage = [context photoWebPageURLFromDictionary:photoDict];
Photo *p = [[PhotoStore sharedStore] createPhoto];
[p setURL:photoURL];
[p setThumbURL:thumbURL];
[p setSource:photoSourcePage];
[p setTitle:title];
NSLog(#"%# - %#", title, [photoURL absoluteString]);
}
[self showGallery];
}

Related

Objective-C: full list of file paths to images and videos in the device

I need to retrive the full list (as comma-separated NSString) of file paths to images and videos stored in an iPhone.
I wrote the following code, currently it gets only images. Can you show me how to improve that code to get also video? Thank you
Please also check if there is something wrong: I have the impression to get less photo then the ones actually stored in the device.
#import <Photos/Photos.h>
-(NSString*)listItems{
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
// NSLog(#"Total: %d",(int)result.count);
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[result count]];
for (PHAsset *asset in result) {
[asset requestContentEditingInputWithOptions:[PHContentEditingInputRequestOptions new] completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *imageURL = contentEditingInput.fullSizeImageURL;
[images addObject : imageURL.absoluteString];
}];
}
NSMutableString * listFiles = [[NSMutableString alloc] init];
for (NSObject * obj in images)
{
[listFiles appendString:[obj description]];
[listFiles appendString:#","];
}
return listFiles;
}
#end

Google directions' URL returning "ZERO RESULTS"

I've followed this tutorial - https://www.youtube.com/watch?v=AdV7bCWuDYg
Trying to draw route on map and I do successfully send latitude/longitude of origin/destination. The problem is its generated URL.
I keep getting this
http://maps.googleapis.com/maps/api/directions/json?&origin=37.661519,127.061106&destination=37.664185,127.062994&sensor=false
But as far as I know, the URL should end with |45.566346,18.667512 something like that. I don't know what I'm doing wrong because the tutorial claims it works and its by Google I think.
- (void)setDirectionsQuery:(NSDictionary *)query withSelector:(SEL)selector
withDelegate:(id)delegate{
NSArray *waypoints = [query objectForKey:#"waypoints"];
NSLog(#"***waypoints : %#", waypoints);
NSString *origin = [waypoints objectAtIndex:0];
int waypointCount = [waypoints count];
int destinationPos = waypointCount -1;
NSString *destination = [waypoints objectAtIndex:destinationPos];
NSString *sensor = [query objectForKey:#"sensor"];
NSMutableString *url =
[NSMutableString stringWithFormat:#"%#&origin=%#&destination=%#&sensor=%#",
kMDDirectionsURL,origin,destination, sensor];
if(waypointCount>2) {
[url appendString:#"&waypoints=optimize:true"];
int wpCount = waypointCount-2;
for(int i=1;i<wpCount;i++){
[url appendString: #"|"];
[url appendString:[waypoints objectAtIndex:i]];
}
}
url = [[url
stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]mutableCopy];
_directionsURL = [NSURL URLWithString:url];
NSLog(#"url : %#", url);
[self retrieveDirections:selector withDelegate:delegate];}
After hours of research I've noticed on Google Directions page that the required URL format is a little different from the one I've been using but still no luck.
[NSMutableString stringWithFormat:#"%#origin=%#&destination=%#&key=[my api key],
kMDDirectionsURL,origin,destination];
Your url should work, I changed the origin to San Francisco and destination to Mountain View and it works:
http://maps.googleapis.com/maps/api/directions/json?&origin=san+francisco&destination=mountain+view&sensor=false
It is possible that Google Maps does not know how to get from 37.661519,127.061106 to 37.664185,127.062994

Display local images in gallery objective-c

I would like to display images that were saved locally from the app to the iPhones Documents directory in a gallery layout, similar to Photos.app with the thumbnails (4 columns, as many rows necessary). I have tried some open sourced code, including CHGridView, but most of them are several years old and not working with iOS 7. I would prefer to not use open sourced code if possible anyway.
I am able to store my files in an NSMutableArray using the following method:
- (NSMutableArray *)arrayOfImages
{
int count;
NSMutableArray *array = [NSMutableArray array];
for (count = 0; count < 500; count++) //assuming no more than 500 items saved.
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [NSString stringWithFormat:#"img%d", count]; //images are called img0.png, img1.png, etc
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
UIImage *checkImage = [[UIImage alloc] initWithContentsOfFile:filePath];
if (checkImage != nil) //if there is an image there, add it to array
{
[array addObject:checkImage];
}
}
return array;
}
UICollectionView was added in iOS6 and is designed to do exactly what you are describing. The code needed is similar to the code for a UITableView, but instead of managing a 1D collection like a table view, the collection view manages a 2D collection of objects.

Running the project by build is not working in MAC OSX 10.7

-(void)artworkImages{
NSArray *noOfSongs = [mySongsArray content];
for (int i=0; i<[noOfSongs count]; i++) {
NSDictionary *dic = [[NSDictionary alloc] init];
dic = [[mySongsArray arrangedObjects] objectAtIndex:i];
NSURL *url = [NSURL fileURLWithPath:[dic objectForKey:#"Path"]];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
for (NSString *format in [asset availableMetadataFormats]) {
for (AVMetadataItem *item in [asset metadataForFormat:format]) {
if (i>240 && i<250) {
NSBeginAlertSheet(#"Check the loop", #"Delete", nil, #"Cancel", window, self, #selector(alertEnd:returnCode:cInfo:), NULL, nil, #"format===%#,%d",format);
}
}
}
}
}
Above methods is working fine if I run the project by Code (X code), but getting issue if I run by build. I am importing songs from iTunes library.xml, after that I am storing into NSArrayController. Here in this method I am trying to fetch artwork images but if I run by build I am getting around 250 images out of 400.
Without the code of alertEnd:returnCode:cInfo: it's hard to tell, but it generally appears like something in that function is killing the for loop.
What exactly are you trying to achieve with:
if (i>240 && i<250)
Can you post the code for the 'alertEnd:returnCode:cInfo'. Also it would probably be easier to read if you used a __block for alert sheets. If the alertEnd:returnCode:cInfo isn't referred to more than once, I'd keep it as a block. If you reuse the code within alertEnd:returnCode:cInfo then promote it to it's own void/function.
some possibilities:
you are importing/reading a different "iTunes library.xml" file that was created with a smaller set of itunes files. and maybe that filename is set in the xcode arguments???
[noOfSongs count] has an inconsistent value, because objects are removed from noOfSongs
try :
NSInteger *cntSongs = [[mySongsArray content] count];
for (int i=0; i< cntSongs; i++) {
rather than the current:
NSArray *noOfSongs = [mySongsArray content];
for (int i=0; i<[noOfSongs count]; i++) {
mySongsArray is shadowed by another property of the same name. see -Wshadow.

load imagesĀ into ikimagebrowser from a folder?

I'm new to cocoa and I have an IKImageBrowserView and that's how I load images to it:
- (IBAction)loadImages:(id)sender
{
NSMutableArray *urls = [[NSMutableArray alloc]init];
int i = 1;
for (i=1; i<55; i++) {
NSString *photoNumber = [NSString stringWithFormat:#"%i", i];
NSMutableString *urlString = [[NSMutableString alloc] initWithString:#"Australia"];
[urlString appendString:photoNumber];
NSURL* url = [[NSBundle mainBundle] URLForImageResource:urlString];
[urls addObject:url];
}
[self addImagesWithPaths:urls];
}
- (void)addAnImageWithPath:(NSString *)path
{
myImageObject *p;
/* add a path to our temporary array */
p = [[myImageObject alloc] init];
[p setPath:path];
[_importedImages addObject:p];
}
- (void)addImagesWithPath:(NSString *)path recursive:(BOOL)recursive
{
NSInteger i, n;
BOOL dir;
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&dir];
if (dir)
{
NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
n = [content count];
// parse the directory content
for (i=0; i<n; i++)
{
if (recursive)
[self addImagesWithPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]] recursive:YES];
else
[self addAnImageWithPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]]];
}
}
else
{
[self addAnImageWithPath:path];
}
}
/* performed in an independant thread, parse all paths in "paths" and add these paths in our temporary array */
- (void)addImagesWithPaths:(NSArray *)urls
{
NSInteger i, n;
n = [urls count];
for ( i= 0; i < n; i++)
{
NSURL *url = [urls objectAtIndex:i];
[self addImagesWithPath:[url path] recursive:NO];
}
/* update the datasource in the main thread */
[self performSelectorOnMainThread:#selector(updateDatasource) withObject:nil waitUntilDone:YES];
}
Now my images are loaded by name - #"Australia". That's kind of inconvenient as your images need to have same name and a number. How do I load images with different names from the folder, which has been imported to xcode?
So at the moment I'm loading images by name Australia1, Australia2, Australia3, Australia4... and so on.
How do I load images from a bundle folder?
Your data source needs to return items to the image browser view that conform to the IKImageBrowserItem protocol. Your myImageObject class is a good place to start with that.
In that protocol, three methods are required:
imageUID: Returns a string that uniquely identifies this item (image).
imageRepresentationType: Returns a constant string that identifies how imageRepresentation represents the image.
imageRepresentation: Returns an object that represents the image.
For a start, I'd just use the path that you're already giving every myImageObject. You can use that as both the identifier string and the image representation.
Depending on what else you're doing in this app, you may find it advantageous later on, for memory and/or speed reasons, to load each image yourself. If, after profiling, you do come to that conclusion, you can load the image yourself as an NSImage or CGImage, change your representation type appropriately, and return the image as the representation.
As the data source, you'll return the number of items in your _importedImages array, and, when asked for the item at an index, return that item via objectAtIndex:.
More info:
The IKImageBrowserDataSource protocol reference
The IKImageBrowserItem protocol reference