I'm using Parse.com as a backend for my iOS app.
I have a class called "User".
In this class I have a flied called "picture" with the users profile picture.
At the moment im kinda stuck on how i can fetch this picture for my currentUser and show it within an UIView (not UIImage) within my storyboard viewcontroller.
Maybe you guys have some sample code for that?
Thanks in Advance!
Amit, sorry your Code didnt work for me, my app crashed.
Here is my solution:
PFUser *cUser = [PFUser currentUser];
PFFile *pictureFile = [cUser objectForKey:#"picture"];
[pictureFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error){
[_currentUserImage setImage:[UIImage imageWithData:data]];
}
else {
NSLog(#"no data!");
[_currentUserImage setImage:[UIImage imageNamed:#"profile"]]; //Set Custom Image if there is no user picture.
}
}];
}
Related
until recently, I've always used Parse.com for data management of my app I was now working on a new project and I have some problems ....
P.S. I'm using Xcode 6
First there PFImageView, you can confirm this? at this point without consulting a PFImageView how can I draw a picture in my app? I do not understand why you can not use in my app PFImageView
Also when I do a query, previously (using the enter button on the keyboard) appeared the auto-build block of the query but now I find myself this
PFQuery *query = [PFUser query];
[query findObjectsInBackgroundWithBlock: (nullable PFArrayResultBlock (nullable) block]
What is happening to parse.com? where am I doing wrong? someone can 'give me help on this?
On the PFImageView:
Try calling – loadInBackground on in. Source
On autocompletion:
This seems to be a bug in Xcode actually. After they have implemented the "nullable" keyword, Xcode seems to be having a hard time auto generating code for the blocks you pass as arguments. That has nothing to do with Parse though.
Why don't use PFFile instead?
For loading an image from data you can do like this:
PFFile *fileImage = your_file;
[fileImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
UIImage *image = [UIImage imageWithData:imageData];
yourImageView.image = image;
}];
And for the PFQuery replace that method with this block:
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { }}];
or check the class reference here
My app is a messaging style app and in it you can "tag" another user. (A bit like twitter).
Now, when this message is displayed, the avatar belonging to the person(s) who was tagged is displayed with that message.
The avatar of the user is stored as a PFFile against the PFUser object.
I'm loading it something like this...
PFImageView *parseImageView = ...
[taggedUser fetchIfNeededInBackgroundWithBlock:^(PFObject *user, NSError *error) {
parseImageView.file = user[#"avatar"];
[parseImageView loadInBackground];
}];
This all works fine.
The load if needed part of the code will most of the time not touch the network as for the majority of the time it has the user data cached.
However, the load in background part that gets the image and puts it into the image view runs every single time. There doesn't seem to be any caching on the PFFile data at all.
Even after downloading the same user's avatar numerous times it still goes to the network to get it.
Is there a way to get this data to cache or is this something I'll have to implement myself?
PFFile will automatically cache the file for you, if the previous PFQuery uses caching policy such as:
PFQuery *query = [PFQuery queryWithClassName:#"MyClass"];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
To check whether the PFFile is in local cache, use:
#property (assign, readonly) BOOL isDataAvailable
For example:
PFFile *file = [self.array objectForKey:#"File"];
if ([file isDataAvailable])
{
// no need to do query, it's already there
// you can use the cached file
} else
{
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
{
if (!error)
{
// use the newly retrieved data
}
}];
}
Hope it helps :)
In the end I created a singleton with an NSCache and queried this before going to Parse.
Works as a quick stop for now. Of course, it means that each new session has to download all the images again but it's a lot better now than it was.
You can cache result of PFQuery like below code..And need to check for cache without finding objects in background everytime..while retrieving the image.It has some other cache policies also..Please check attached link also..
PFQuery *attributesQuery = [PFQuery queryWithClassName:#"YourClassName"];
attributesQuery.cachePolicy = kPFCachePolicyCacheElseNetwork; //load cache if not then load network
if ([attributesQuery hasCachedResult]){
NSLog(#"hasCached result");
}else{
NSLog(#"noCached result");
}
Source:https://parse.com/questions/hascachedresult-always-returns-no
Hope it helps you....!
I have an iOS app that integrated with FBLoging. And I know it can assign the profile picture into a UIView like this.
// This method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
self.profilePictureView.profileID = user.id;
}
But what I wanna do is to get that profile picture into my already available UIImageView. In order to do this I have a Singleton class. I want to get that image as a UIImage and assign to the variable inside that Singleton class. When another Viewcontroller load I want to assign that singleton class's UIImage into my Viewcontroller's UIImageView
How can I do this. please help me.
Thank you
If you have Facebook user.id with you, you can create the imageURL like this
NSString * url = [NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large",user.objectID];
Then save this url to your singleton class. And you can download like this if you need.(You can download each time you required or download once and save in to a file, if required ,fetch from file).
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
UIImage *userImage = [UIImage imageWithData:data];
You can get the image from the profilePictureImageView using this answer .
Once you have the image, then you can save it in your Singleton class as is or you can assign it to your imageView.
The method in the link only works once the image has been downloaded into the profilePictureImageView. So you need to take care of that, maybe by waiting. Or keep checking it for non nil value after some time in succession.
you can try following method
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *FBuser, NSError *error) {
if (error) {
// Handle error
}
else {
NSString *userName = [FBuser name];
NSString *userImageURL = [NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large", [FBuser id]];
}
}];
you have to store this url into your singleton class
I am using Parse. I have a PFFILE that I am retrieving using a Query. I need to save it, and i found that you normally use saveEventualy. But it doesn't support PFFile. So how can I turn the PFFile into a PFObject? Or else how save the image for offline? That's my code up to now:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self GetImage];
}
- (void)enteredForeground:(NSNotification*) not
{
[self GetImage];
}
-(void)GetImage
{
PFQuery *query = [PFQuery queryWithClassName:#"Image"];
[query getObjectInBackgroundWithId:#"4tmub1uxVd" block:^(PFObject *imageObject, NSError >*error)
{
if (imageObject) {
PFFile *imageFile = imageObject[#"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (data) {
UIImage *image = [UIImage imageWithData:data];
if (image) {
self.imageview.image = image;
}
} else {
NSLog(#"Error fetching image file: %#", error);
}
}];
} else {
NSLog(#"Error fetching object: %#", error);
}
}];
}
Parse has recently introduced a new method called local Data Store. It let's you store objects and files, update and retrieve them. Check out the documentation.
Blog Post
Documentation
That doesn't exactly answer your question, but it will achieve what you want it to!
You can't convert a PFFile to a PFObject, but you don't need to. The Image PFObject class you're fetching in the code above has a property, with key image, that represents a PFFile. If you modify this, you'd save the parent object, which would save the updated file alongside it.
I am working on Game Center for iOS devices using the sandbox environment. I am trying to load the achievement images I set up in iTunes Connect.
I am using loadAchievementDescriptionWithConpletionHandler successfully.
The problem happens when I try to load the image using loadImageWithCompletionHandler.
[self.description loadImageWithCompletionHandler:^(UIImage *image, NSError *error) {
if (!error) {
self.achievementImage = image;
}
}];
The problem is both the UIImage and the error are nil.
It is due to the sandbox environment ? Am I missing something ?
Thanks
I can load the image successfully. It looks like my image format was wrong.
I have deleted the achievement and created a new one using a 512x512 72 dpi image.
Using loadAchievementDescriptionsWithCompletionHandler. I load all the achievementDescriptions from Game Center. Then, I use loadImageWithCompletionHandler to extract the image.
[GKAchievementDescription loadAchievementDescriptionsWithCompletionHandler:^(NSArray *descriptions, NSError *error) {
if (!error) {
for (GKAchievementDescription *achievementDescription in descriptions) {
[self.achievementDescriptionDictionary setObject:achievementDescription
forKey:achievementDescription.identifier];
[achievementDescription loadImageWithCompletionHandler:^(UIImage *image, NSError *error) {
if (!error) {
if (image) {
UIImage *achievementimage = image;
}
}
}];
}
}
}];