How to convert a batch of PFFiles to UImages? - ios

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.

Related

How to use sdimage in uiimage caching?

Anyway to handle or display UIImage with caching and with placeholder?
I have found one answer from stack exchange but it didnot work for me given below the link :
Best way to cache images on ios app?
Notes: I dont have any URL because i get image object of PHIMAGE asset library.
You can use SDWebImage
It is very simple to display and cache image.
[imageView sd_setImageWithURL:[NSURL URLWithString:#"http://www.image_url.com"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
You can use a very usefull library called SDWebImage
SDWebImage automagically cache all your images when you provide a valid URL with the sd_setImageWithURL. So the next time you call it, will use the cache system instead of re-download the image.
Example to implement in a UIImageView from their github:
// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
There was a problem of smoothing an all image while scroll the table but there is an issue of reload the cell not the caching problem i solved this issue.
I get an image one by one from local identifier which is provided by PHAsset framework and display that image into cell.
For reference Code:
PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:#[your local identifier] options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
//this gets called for every asset from its localIdentifier you saved
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
imageRequestOptions.deliveryMode = PHImageRequestOptionsResizeModeFast;
[[PHImageManager defaultManager]requestImageForAsset:asset targetSize:CGSizeMake(50,50) contentMode:PHImageContentModeAspectFill options:imageRequestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(#"You get an image from result");
}];
}];

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

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];

cached NSData object can't be used after retrieval

I am caching an NSData object containing image data retrieved from the web. The image displays correctly before caching. When I retrieve the data object from the cache, the data can no longer be used to create a UIImage, even though the data objects are identical.
Please see the relevant snippets of my code below
NSData *webData= [NSData dataWithContentsOfURL:webPath]; //retrieve from web
UIImage *webImage = [UIImage imageWithData:webData]; //works fine
[webData writeToURL:filePath atomically:YES]; //cache
NSData *cacheData = [NSData dataWithContentsOfURL:filePath]; //get from cache
if ([cacheData isEqualToData:webData]) NSLog(#"Equal"); //Data objects are equal
UIImage *cacheImage = [UIImage imageWithData:cacheData]; //cacheImage is nil
I can fix the problem by changing the way I store my data to the cache
NSData *temp = UIImageJPEGRepresentation(webImage, 1.0):
[temp writeToURL:filePath atomically:YES];
Now the webData and cacheData are no longer equal, but cacheImage is not nil and displays properly.
EDIT - After a bit more testing, I realized I get the same problem using UIImageJPEGRepresentation as well.
Anyone know why this would be?
Thanks.
Figure out the problem was that I was trying to do all this before my view controller was fully loaded.

iOS: TableView with multiple Images per Row (SDWebImage)

I am a bit desperated about my sectioned tableview. I use a custom UITableViewCell with 4 images like the one below:
I try to load the images via SDWebImage for each cell.
The loading procedures are all done in my custom UITableViewCell - not in the UITableViewController. From the cellForRowAtIndexPath i just call [cell setup] which executes the following code in the current cell:
NSArray *imageViews = [NSArray arrayWithObjects:self.imageView1, self.imageView2, self.imageView3, self.imageView4, nil];
for (int i = 0; i < self.products.count; i++) {
Product *currentProduct = [self.products objectAtIndex:i];
UIImageView *currentImageView = [imageViews objectAtIndex:i];
NSString *thumbURL = [[CommonCode getUnzippedDirectory] stringByAppendingPathComponent:currentProduct.collectionFolderName];
thumbURL = [thumbURL stringByAppendingPathComponent:thumbFolder];
thumbURL = [thumbURL stringByAppendingPathComponent:currentProduct.product_photo];
[currentImageView setContentMode:UIViewContentModeScaleAspectFit];
[currentImageView setImageWithURL:[NSURL fileURLWithPath:thumbURL]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
The images are all stored in the documents directory and are not greater than max. 500Kb.
Problem:
My Problem is that when I scroll through my tableview it suddenly crashes and I don't know why. Enabling a symbolic breakpoint for all exceptions shows that it crashes because of one line in SDWebImage. Unfortunately there isn't a debugger output: (It crashes where the image is allocated)
UIImage *image = nil;
if ([imageOrData isKindOfClass:[NSData class]])
{
image = [[UIImage alloc] initWithData:(NSData *)imageOrData];
}
I also tried to load images via dispatch_asnyc with a similar result. Is it possible that it has something to do with concurrent file operations?
Additionally I get Memory Warnings when I scroll very fast so that I have to clear the cache of SDWebImage. At the same time it stops at the code line in SDWebImage mentioned above.
I already searched the web for 2 days now and I didn't find something useful. I would be glad for some hints to fix this problem. If somebody needs additional data such as crash reports or something, just let me know and I will provide them quickly.
I have meet the same problem, But I solve it like below temporarily, but it cause memory problem especially on iPhone 4s
NSArray *arrImgs = cellModel.thumbnails_qqnews;
if (arrImgs.count > 0) {
[self.imgView.subviews enumerateObjectsUsingBlock:^(__kindof UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj sd_setImageWithURL:[NSURL URLWithString:arrImgs[idx]] placeholderImage:[UIImage imageNamed:#"placeholdImage"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image != nil) {
obj.image = [UIImage cutImageWithTargetSize:[NEMulTiNewsCell getImgSize] image:image];
}
}];
}];
}
I think multi images download is illogical,but the method i think is download multi images then draw multi images in one image then display, but it may create large number line of code.

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