How to pull saved images from Parse.com into my app - ios

I would like to save some images in Parse Server and like to pull it down into my iOS app. Any idea how to do it?

Use a PFImageView where you'd normally use a UIImageView. Give it the PFFile for the image you wish to display, then call loadInBackground.
PFFile *imageFile = [pfObject objectForKey:#"image"];
PFImageView *imageView = [[PFImageView alloc] init];
imageView.file = imageFile;
[imageView loadInBackground];

Related

Unable to load images from image urls in App Notification Widget

I am trying to show list of images in a collection view inside App Notification Widget.
Its giving me the error "Unable to load" if I am loading from urls, other than its working fine.
Can anyone please help me in this ?
TodaysCusCell *catTrendingCusCell = (TodaysCusCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"TodaysCusCellID" forIndexPath:indexPath];
NSDictionary *trendingDic = (NSDictionary *)[trendingArr objectAtIndex:indexPath.row];
//Loading Images
NSString *imageURL = [trendingDic objectForKey:#"image_url"];
[catTrendingCusCell.trendingImgView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:#"grayBackground"]];

How to convert a batch of PFFiles to UImages?

In my app, images are being stored on parse. They then get retrieved into an array of PFFiles, however I have to convert them individually to UIImages using this method which takes time.
PFFile *picture = [pictureArray objectAtIndex:indexPath.row];
[picture getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
myimage = [UIImage imageWithData:data];
}];
So I need to store images on parse, and retrieve them, and I don't want to individually convert all of these pictures, I want it all done at once. These are the options I currently see:
Create a loop that loops the code above to convert all the PFFiles to UIImages and add them to an array ready to access.
Find a better way to convert or use PFFiles.
Upload NSData or something else to parse so I don't have to convert the PFFiles.
Can anyone provide me advice or solutions to my problem? As I am presenting a fairly large amount of images in a UICollectionView, I want them to be all loaded at once some how instead of having to load them individually, I want the images in an array ready to go so I can just call:
cell.myimage.image = [pictureArray objectAtIndex:indexPath.row];
and all the images will be loaded instead of waiting for PFFIles to be converted etc.
If you want to present them you should use PFImageView which can load the by itself. It's a subclass of UIImageView and its pretty.
Example:
PFImageView *creature = [[PFImageView alloc] init];
creature.image = [UIImage imageNamed:#”1.jpg”]; // placeholder image
creature.file = (PFFile *)file;
[creature loadInBackground];
If you want to download them for other uses check out this answer.

Display Image fetched from the WebService

How to display an Image in the ImageView which is fetched from the WebService?
I am able to retrieve the Name of the image from the service successfully, but unable to set the image in the ImageView
NSString *imgName = [students objectForKey:#"Image"];
NSLog(#"%#", imgName); **/* Image1.png */**
imgImage.image = [UIImage imageNamed:imgName];
NSLog gives following output on Console :
Image1.png
I am unable to set the image in the ImageView
You can use the SDWebImage :
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
This is the link for download and how to use it:
https://github.com/rs/SDWebImage
You can also use this code set image
cell.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"your url"]]]];

copy UIImageView that pulls from setImageWithURL

before i had been pulling the same image twice from my server by using, which worked fine but i need to cut down network usage
NSString *friendAvatar = [NSString stringWithFormat:#"%#%#%#", #"http://www.mydomain.com/images/users/", myWords[0], #".jpg"];
[imageFile setImageWithURL:[NSURL URLWithString:friendAvatar]];
[bgImageFile setImageWithURL:[NSURL URLWithString:friendAvatar]]; //this is a zoomed in version of the friends photo
now i am using this way to attempt to pul the image of the UIImageView that already pulled the photo, that way i dont have to be pulling the same photo twice...
NSString *friendAvatar = [NSString stringWithFormat:#"%#%#%#", #"http://www.mydomain.com/images/users/", myWords[0], #".jpg"];
[imageFile setImageWithURL:[NSURL URLWithString:friendAvatar]];
[bgImageFile setImage:imageFile.image];
when trying to use my new method. nothing happens. no error in the debugger, the background image just simply is blank.
Based on your comment I discovered that because you are using AFNetworking when you call
[imageFile setImageWithURL:[NSURL URLWithString:friendAvatar]];
it is being executed on a background thread, but the next line
[bgImageFile setImage:imageFile.image];
is not a AFNetworking call so it is being executed before the previous line has completed and thus there is no imageFile.image to use...
So, yes, my previous answer requires you to do the async code yourself, or you could wait for the image to load before setting bgImageFile.image (Which might be done with KVO???)
Try creating a UIImage first then set UIImageView.image to the created UIImage...
UIImage *avatarImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:friendAvatar]]];
[imageFile setImage:avatarImage];
[bgImageFile setImage:avatarImage];
And a better way would be...
dispatch_queue_t myQueue = dispatch_queue_create("com.myProgram.myQueue", NULL);
dispatch_async(myQueue, ^{
UIImage *avatarImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:friendAvatar]]];
dispatch_async(dispatch_get_main_queue(), ^{
[imageFile setImage:avatarImage];
[bgImageFile setImage:avatarImage];
});
});
This will load the file from the internet on a background thread and then update the ImageViews on the main thread when the image is done loading. The benefit is that your app won't freeze up during downloading.
I hope that helps

How to get a UIImage object from a asset url

I am using ALAsset framework for selecting multiple images from the image library. I want to store the selected images into the database. These stored images are then to be used to show the slideshow of the images. My problem i am storing the image URL into the database, URL is in format - assets-library://asset/asset.JPG?id=1000000007&ext=JPG. Now for displaying the images i need UIImage object, so how do i create a UIImage object from this kind of URL. I have tried -
for(NSDictionary *dict in info)
{
NSString* path = [[NSString alloc] initWithString:[dict objectForKey:#"UIImagePickerControllerReferenceURL"]];
UIImage* ui;
ui = [[UIImage alloc] initWithContentsOfFile:path];
}
This did not worked. Please some one tell me how do i do it..

Resources