Find random Facebook user picture via Graph API [duplicate] - objective-c

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
objective c facebook graph return false
I am building an app that returns a random Facebook profile picture. So far I have the code below generating a random profile ID which sometimes does return a real profile but sometimes doesn't and just shows the generic blue Facebook face. When I use the given number on the actual website graph API it just returns false. My question is how would I put the code in so that if the random number generated returns a false profile, it just keeps generating a new random number until a real profile is returned, thus a real picture?
#implementation FacebookPicturesViewController
- (IBAction) nextImagePush {
NSString *prefix = #"http://graph.facebook.com/";
NSString *profileId = [NSString stringWithFormat:#"%09d", abs(arc4random())];
NSLog(#"profileId: %#", profileId);
NSString *suffix = #"/picture?type=large";
NSString* url= [NSString stringWithFormat:#"%#%#%#", prefix, profileId, suffix];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
[imageView setImage:img];
imageCount++;
NSLog(#"profileId: %#", profileId);
if (imageCount >= [imageArray count]){
imageCount = 0;
}
}

'false' usually indicates a privacy check failed.
Check that the user hasn't blocked you or the app, and check they haven't disabled Platform entirely, deactivated their account, etc.

Related

Save multiple images quickly in iOS 6 (custom album)

I'm writing an application that will take several images from URL's, turn them into a UIImage and then add them to the photo library and then to the custom album. I don't believe its possible to add them to a custom album without having them in the Camera Roll, so I'm accepting it as impossible (but it would be ideal if this is possible).
My problem is that I'm using the code from this site and it does work, but once it's dealing with larger photos it returns a few as 'Write Busy'. I have successfully got them all to save if I copy the function inside its own completion code and then again inside the next one and so on until 6 (the most I saw it take was 3-4 but I don't know the size of the images and I could get some really big ones) - this has lead to the problem that they weren't all included in the custom album as they error'd at this stage too and there was no block in place to get it to repeat.
I understand that the actual image saving is moved to a background thread (although I don't specifically set this) as my code returns as all done before errors start appearing, but ideally I need to queue up images to be saved on a single background thread so they happen synchronously but do not freeze the UI.
My code looks like this:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:singleImage]]];
[self.library saveImage:image toAlbum:#"Test Album" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Error");
}
}];
I've removed the repetition of the code otherwise the code sample would be very long! It was previously where the NSLog code existed.
For my test sample I am dealing with 25 images, but this could easily be 200 or so, and could be very high resolution, so I need something that's able to reliably do this over and over again without missing several images.
thanks
Rob
I've managed to make it work by stripping out the save image code and moving it into its own function which calls itself recursively on an array on objects, if it fails it re-parses the same image back into the function until it works successfully and will display 'Done' when complete. Because I'm using the completedBlock: from the function to complete the loop, its only running one file save per run.
This is the code I used recursively:
- (void)saveImage {
if(self.thisImage)
{
[self.library saveImage:self.thisImage toAlbum:#"Test Album" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
[self saveImage];
}
else
{
[self.imageData removeObject:self.singleImageData];
NSLog(#"Success!");
self.singleImageData = [self.imageData lastObject];
self.thisImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.singleImageData]]];
[self saveImage];
}
}];
}
else
{
self.singleImageData = nil;
self.thisImage = nil;
self.imageData = nil;
self.images = nil;
NSLog(#"Done!");
}
}
To set this up, I originally used an array of UIImages's but this used a lot of memory and was very slow (I was testing up to 400 photos). I found a much better way to do it was to store an NSMutableArray of URL's as NSString's and then perform the NSData GET within the function.
The following code is what sets up the NSMutableArray with data and then calls the function. It also sets the first UIImage into memory and stores it under self.thisImage:
NSEnumerator *e = [allDataArray objectEnumerator];
NSDictionary *object;
while (object = [e nextObject]) {
NSArray *imagesArray = [object objectForKey:#"images"];
NSString *singleImage = [[imagesArray objectAtIndex:0] objectForKey:#"source"];
[self.imageData addObject:singleImage];
}
self.singleImageData = [self.imageData lastObject];
self.thisImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.singleImageData]]];
[self saveImage];
This means the rest of the getters for UIImage can be contained in the function and the single instance of UIImage can be monitored. I also log the raw URL into self.singleImageData so that I can remove the correct elements from the array to stop duplication.
These are the variables I used:
self.images = [[NSMutableArray alloc] init];
self.thisImage = [[UIImage alloc] init];
self.imageData = [[NSMutableArray alloc] init];
self.singleImageData = [[NSString alloc] init];
This answer should work for anyone using http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/ for iOS 6 (tested on iOS 6.1) and should result in all pictures being saved correctly and without errors.
If saveImage:toAlbum:withCompletionBlock it's using dispatch_async i fear that for i/o operations too many threads are spawned: each write task you trigger is blocked by the previous one (bacause is still doing I/O on the same queue), so gcd will create a new thread (usually dispatch_async on the global_queue is optimized by gcd by using an optimized number of threads).
You should either use semaphores to limit the write operation to a fixed number at the same time or use dispatch_io_ functions that are available from iOS 5 if i'm not mistaken.
There are plenty example on how to do this with both methods.
some on the fly code for giving an idea:
dispatch_semaphore_t aSemaphore = dispatch_semaphore_create(4);
dispatch_queue_t ioQueue = dispatch_queue_create("com.customqueue", NULL);
// dispatch the following block to the ioQueue
// ( for loop with all images )
dispatch_semaphore_wait(aSemaphore , DISPATCH_TIME_FOREVER);
[self.library saveImage:image
toAlbum:#"Test Album"
withCompletionBlock:^(NSError *error){
dispatch_semaphore_signal(aSemaphore);
}];
so every time you will have maximum 4 saveImage:toAlbum, as soon as one completes another one will start.
you have to create a custom queue, like above (the ioQueue) where to dispatch the code that does the for loop on the images, so when the semaphore is waiting the main thread is not blocked.

Displaying a photo from url

I want to get a photo from my homepage and display it. And it (kind of) works. But sometimes it takes min 10 seconds to load the next scene because of something that happens here. So here is what I do :
NSString *myURL = [PICURL stringByAppendingString:[[[[levelConfig objectForKey:category] objectForKey:lSet] objectForKey:levelString] objectForKey:#"pic"]];
UIImage *dYKPic = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:myURL]]];
if(dYKPic == nil){
NSString *defaultURL = #"http://www.exampleHP.com/exampleFolder/default.jpg";
dYKPic = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:defaultURL]]];
}
CCTexture2D *tex = [[CCTexture2D alloc] initWithImage:dYKPic];
CCSprite *image = [CCSprite spriteWithTexture:tex];
image.anchorPoint = ccp(0,0);
image.position = ccp(32,216);
[self addChild:image z:2];
So, it takes 10 seconds, and additionally, the default.jpg is loaded - even though the picture exists - but that just in the case where it takes so long... 70% of the cases it works perfectly normal... So what is wrong ? Where do I release tex ? Immediately after adding the child ?
It has to load the picture. Thats the issue. You either need to load and cache it, store it, or preload it before you need it.
Or one final option is the load it async and just update your view when its finished.

How Can I Save & Retrieve an image (bytes) to SQLite (blob) using FMDB?

I'm making an iOS App that need to show some images from a remote site (from an URL), and everytime the users enter to the screen that should show the image, the app get freeze until the download is completed. So I want to store the images already downloaded into a SQLite Table named COVERS.
Here is the code that how I'm downloading and Showing the image:
Suppose that movieCover is an UIImageView and the object movie has a NSURL property named cover that contains the URL of the image to be downloaded.
NSData *cover = [[NSData alloc] initWithContentsOfURL:movie.cover];
movieCover.image = [[UIImage alloc] initWithData:cover];
But, I want to change it to something like this:
NSData *cover = [appDelegate.dataBase getCoverForMovie:movie];
if( !cover ) {
cover = [[NSData alloc] initWithContentsOfURL:movie.cover];
[appDelegate.dataBase setCover:cover ToMovie:movie];
}
movieCover.image = [[UIImage alloc] initWithData:cover];
Suppose that appDelegate is a property of the current ViewController, and dataBase is a property of the AppDelegate wich uses FMDB to manipulate the data in the DataBase.
I need to get the cover previously saved in the database using the method:
- (NSData *)getCoverForMovie:(Movie *)movie;
But, if there is not a cover saved, then return nil.
So I need to save the cover using the method
- (BOOL)saveCover:(NSData *)cover ForMovie:(Movie *)movie;
But I don't know how to code this method. Need some help with it.
Methods Implementations based on fmdb.m examples
- (NSData *)getCoverForMovie:(Movie *)movie
{
NSData *cover = nil;
FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
[db open];
FMResultSet *results = [db executeQueryWithFormat:#"SELECT * FROM COVERS WHERE movie = %i", movie.movieID];
if([results next])
{
cover = [results dataForColumn:#"cover"];
}
return cover;
}
- (BOOL)saveCover:(NSData *)cover ForMovie:(Movie *)movie
{
BOOL result;
FMDatabase *db = [FMDatabase databaseWithPath:databasePath];
[db open];
result = [db executeUpdate:#"INSERT OR REPLACE INTO COVERS (movie, cover) VALUES (?,?)", movie.movieID, cover];
return result;
}
Thanks to #ccgus for his answer.
Check out main.m in the FMDB distribution- it shows how to save and pull out a binary blob (using the safari icon as an example)".

Use Facebook API with Objective C to find random Facebook user image

I am building an app that returns a random Facebook profile picture. So far I have the code below generating a random profile ID which sometimes does return a real profile but sometimes doesnt and just shows the generic blue Facebook face. When I use the given number on the actual website graph API it just returns false. My question is how would I put the code in so that if the random number generated returns a false profile, it just keeps generating a new random number until a real profile is returned, thus a real picture? Thanks in advance
#implementation FacebookPicturesViewController
- (IBAction) nextImagePush {
NSString *prefix = #"http://graph.facebook.com/";
NSString *profileId = [NSString stringWithFormat:#"%09d", abs(arc4random())];
NSLog(#"profileId: %#", profileId);
NSString *suffix = #"/picture?type=large";
NSString* url= [NSString stringWithFormat:#"%#%#%#", prefix, profileId, suffix];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
[imageView setImage:img];
imageCount++;
NSLog(#"profileId: %#", profileId);
if (imageCount >= [imageArray count]){
imageCount = 0;
}
}
It sounds like your problem is that you can't tell if it's a real image or not because Facebook always returns a default image?
Instead of just requesting the picture?type=large directly (which always returns an image), first make a request to the http://graph.facebook.com/USERID and read that response - which will be false or something if it's not a real user.
Loop through until you find a real user, then request the image URL.
I'm not sure why you would want to do this though... but good luck.

How to get [UIImage imageWithContentsOfFile:] and High Res Images working

As many people are complaining it seems that in the Apple SDK for the Retina Display there's a bug and imageWithContentsOfFile actually does not automatically load the 2x images.
I've stumbled into a nice post how to make a function which detects UIScreen scale factor and properly loads low or high res images ( http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/ ), but the solution loads a 2x image and still has the scale factor of the image set to 1.0 and this results to a 2x images scaled 2 times (so, 4 times bigger than what it has to look like)
imageNamed seems to accurately load low and high res images, but is no option for me.
Does anybody have a solution for loading low/high res images not using the automatic loading of imageNamed or imageWithContentsOfFile ? (Or eventually solution how to make imageWithContentsOfFile work correct)
Ok, actual solution found by Michael here :
http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/
He figured out that UIImage has the method "initWithCGImage" which also takes a scale factor as input (I guess the only method where you can set yourself the scale factor)
[UIImage initWithCGImage:scale:orientation:]
And this seems to work great, you can custom load your high res images and just set that the scale factor is 2.0
The problem with imageWithContentsOfFile is that since it currently does not work properly, we can't trust it even when it's fixed (because some users will still have an older iOS on their devices)
We just ran into this here at work.
Here is my work-around that seems to hold water:
NSString *imgFile = ...path to your file;
NSData *imgData = [[NSData alloc] initWithContentsOfFile:imgFile];
UIImage *img = [[UIImage alloc] initWithData:imgData];
imageWithContentsOfFile works properly (considering #2x images with correct scale) starting iOS 4.1 and onwards.
Enhancing Lisa Rossellis's answer to keep retina images at desired size (not scaling them up):
NSString *imagePath = ...Path to your image
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath] scale:[UIScreen mainScreen].scale];
I've developed a drop-in workaround for this problem.
It uses method swizzling to replace the behavior of the "imageWithContentsOfFile:" method of UIImage.
It works fine on iPhones/iPods pre/post retina.
Not sure about the iPad.
Hope this is of help.
#import </usr/include/objc/objc-class.h>
#implementation NSString(LoadHighDef)
/** If self is the path to an image, returns the nominal path to the high-res variant of that image */
-(NSString*) stringByInsertingHighResPathModifier {
NSString *path = [self stringByDeletingPathExtension];
// We determine whether a device modifier is present, and in case it is, where is
// the "split position" at which the "#2x" token is to be added
NSArray *deviceModifiers = [NSArray arrayWithObjects:#"~iphone", #"~ipad", nil];
NSInteger splitIdx = [path length];
for (NSString *modifier in deviceModifiers) {
if ([path hasSuffix:modifier]) {
splitIdx -= [modifier length];
break;
}
}
// We insert the "#2x" token in the string at the proper position; if no
// device modifier is present the token is added at the end of the string
NSString *highDefPath = [NSString stringWithFormat:#"%##2x%#",[path substringToIndex:splitIdx], [path substringFromIndex:splitIdx]];
// We possibly add the extension, if there is any extension at all
NSString *ext = [self pathExtension];
return [ext length]>0? [highDefPath stringByAppendingPathExtension:ext] : highDefPath;
}
#end
#implementation UIImage (LoadHighDef)
/* Upon loading this category, the implementation of "imageWithContentsOfFile:" is exchanged with the implementation
* of our custom "imageWithContentsOfFile_custom:" method, whereby we replace and fix the behavior of the system selector. */
+(void)load {
Method originalMethod = class_getClassMethod([UIImage class], #selector(imageWithContentsOfFile:));
Method replacementMethod = class_getClassMethod([UIImage class], #selector(imageWithContentsOfFile_custom:));
method_exchangeImplementations(replacementMethod, originalMethod);
}
/** This method works just like the system "imageWithContentsOfFile:", but it loads the high-res version of the image
* instead of the default one in case the device's screen is high-res and the high-res variant of the image is present.
*
* We assume that the original "imageWithContentsOfFile:" implementation properly sets the "scale" factor upon
* loading a "#2x" image . (this is its behavior as of OS 4.0.1).
*
* Note: The "imageWithContentsOfFile_custom:" invocations in this code are not recursive calls by virtue of
* method swizzling. In fact, the original UIImage implementation of "imageWithContentsOfFile:" gets called.
*/
+ (UIImage*) imageWithContentsOfFile_custom:(NSString*)imgName {
// If high-res is supported by the device...
UIScreen *screen = [UIScreen mainScreen];
if ([screen respondsToSelector:#selector(scale)] && [screen scale]>=2.0) {
// then we look for the high-res version of the image first
UIImage *hiDefImg = [UIImage imageWithContentsOfFile_custom:[imgName stringByInsertingHighResPathModifier]];
// If such high-res version exists, we return it
// The scale factor will be correctly set because once you give imageWithContentsOfFile:
// the full hi-res path it properly takes it into account
if (hiDefImg!=nil)
return hiDefImg;
}
// If the device does not support high-res of it does but there is
// no high-res variant of imgName, we return the base version
return [UIImage imageWithContentsOfFile_custom:imgName];
}
#end
[UIImage imageWithContentsOfFile:] doesn't load #2x graphics if you specify an absolute path.
Here is a solution:
- (UIImage *)loadRetinaImageIfAvailable:(NSString *)path {
NSString *retinaPath = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:#"%##2x.%#", [[path lastPathComponent] stringByDeletingPathExtension], [path pathExtension]]];
if( [UIScreen mainScreen].scale == 2.0 && [[NSFileManager defaultManager] fileExistsAtPath:retinaPath] == YES)
return [[[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:retinaPath]] CGImage] scale:2.0 orientation:UIImageOrientationUp] autorelease];
else
return [UIImage imageWithContentsOfFile:path];
}
Credit goes to Christof Dorner for his simple solution (which I modified and pasted here).