displaying image in table view which is saved in document directory - ios

i have saved image in document directory with following code and after it image is there in documentary directory successfully
UIImage *image =[[UIImage alloc ]init ];
image = [UIImage imageNamed:#"PlaceHolder.png"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString: #"PlaceHolder.png"] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
now i want to display image in table view
as in my table view some images are getting displayed from facebook url thats why i am using following code
NSString *path = [[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]];
NSURL *url = [NSURL URLWithString:path];
AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease];
imageView.frame = CGRectMake(0, -5, 45, 45);
imageView.imageURL=url;
_arrayForTable is array of Dic and it includes URL some links are from facebook and some are from document directory
now my problem is as follows
** Only facebook images are getting displayed in table view but document directory images are not getting displayed**
i have checked location are perfectly correct for image but now displaying

NSURL *url = [NSURL URLWithString:path];
This is wrong in your code. Replace it with this line of code.
NSURL *url = [NSURL fileURLWithPath:path];
Check if file is exists in your documents directory or not
if([[NSFileManager defaultManager] fileExistsAtPath:path])
NSURL *url = [NSURL fileURLWithPath:path];
Also check in your documents directory if the image is getting saved or not. You are given the name of the image like this
image = [UIImage imageNamed:#"PlaceHolder.png"];
so if you are repeating this line your images will be overwritten in documents directory.
You can also put a check here
BOOL success = [data writeToFile:path atomically:YES];
if(success)
NSLog(#"image written successfully");
else
NSLog(#"image writing failed");
Hope I helped.

In your .m file
#interface YourClass ()<NSURLConnectionDelegate>
#property (nonatomic, copy) NSMutableData *receivedImageData;
#end
#implementation YourClass
- (void)init {
if (self = [super init]) {
// Do your init stuff
_receivedImageData = [[NSMutableData alloc] initWithCapacity:0];
}
}
// A method to start your connection
- (void)startDownloadingImage {
NSMutableURLRequest *downloadImageRequest = [[NSMutableURLRequest alloc] init];
downloadImageRequest.URL = [NSURL URLWithString:#"http://www.theImage.you/want/to/download.extension"];
downloadImageRequest.HTTPMethod = #"GET";
[NSURLConnection connectionWithRequest:downloadImageRequest delegate:self];
}
// You need the following delegate methods
#pragma mark -
#pragma mark NSURLConnection delegate methods
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// This method is called frequently ... you can see if you log
// NSLog(#"I am downloading");
[self.receivedImageData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Log an error or do whatever you want ;)
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Create your picture here using
UIImage *image = [UIImage imageWithData:self.receivedImageData scale:1.0];
// Only if there is an image display the downloaded image
if (CGSizeEqualToSize(self.imageView.image.size, CGSizeZero)) {
self.imageView.image = image;
}
}
You could try to download the image, if there is no image you can keep displaying the PlaceHolder.png
Make sure you copied the image and it really is in your project directory
// self.imageView is an UIImageView
self.imageView.image = [UIImage imageNamed:#"PlaceHolder.png"];
[self startDownloadingImage];
Let me know if it helped you ;)

Did you check the image data is not nil for the Document directory image?you have to retrieve the image by correct path and make sure that you are giving correct positions

AsyncImageView is a subclass of UIImageView. You can simply set the image property after you load the image using initWithContentsOfFile:filePath. You just have to figure out which images you want to load from the directory, and which ones from the web.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]];
NSURL *url = [NSURL URLWithString:path];
if ([path rangeOfString:documentsDirectory].location != NSNotFound)
url = [NSURL fileURLWithPath:path isDirectory:NO];
AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease];
imageView.frame = CGRectMake(0, -5, 45, 45);
imageView.imageURL=url;

Does Asynchimageview support loading image from document directory or the bundle with image url??..Try to load an image from the bundle first. Recently i had a similar problem and ended up loading a placeholder from server itself.
Like in some other answers you can first create a UIImage object with initWithContentsOfFile: set asynchimageView.image.

Related

Download images asynchronously

I am using the following code to download images. Can someone confirm the images are being downloaded asynchronously as they would appear to be? Normally, they download rapidly but every now and then the UI freezes for a minute while data comes through so something would appear to be awry:
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
NSString *picURL = [NSString stringWithFormat:#"http://~/pics/%#",picname];
NSURL *urlPicUrl = [NSURL URLWithString:picURL];
dispatch_async(kBgQueue, ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:picURL]];
if (imgData) {
UIImage *imageCache = [[UIImage alloc] init];
imageCache = [UIImage imageWithData:imgData];
if (imageCache) {
[self saveImage:imageCache asPicName:picname];
dispatch_async(dispatch_get_main_queue(), ^{
});
}
}
});
EDIT:
Here is code to save image.
- (void)saveImage: (UIImage*)image asPicName: (NSString*)picname
{
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithString: picname] ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
}

upload multiple images to ftp server

I am currently working IOS application which can be used to upload multiple image to ftp server. I am using GoldRaccoon class file. I have selected 5 images, but it only uploads the last image.
- (void)zcImagePickerController:(ZCImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(NSArray *)info {
[self dismissPickerView];
NSString *fullPath;
for (NSDictionary *imageDic in info) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[imageDic objectForKey:UIImagePickerControllerOriginalImage]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[arrImageView addObject:imageView];
UIImage *image1 = [imageDic objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData * imageData1 = UIImageJPEGRepresentation(image1,100); //convert image into .png format.
NSFileManager * fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray * paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString * documentsDirectory = [paths1 objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpg",image1]]; //add our image to the path
// NSString *str=[NSString stringWithFormat:#"%#.jpg",image1];
[fileManager createFileAtPath:fullPath contents:imageData1 attributes:nil]; //finally save the image
[arrvalue addObject:fullPath];
NSURL *imageURL = [imageDic valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock =^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
NSString *fileName = [representation filename];
// Send WebService
// [arrName addObject:fileName];
[self sendImage:fileName :fullPath];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init] ;
[assetslibrary assetForURL:imageURL resultBlock:resultblock failureBlock:nil];
}
}

How to make a URL load request timeout?

Im using this tutorial to load an image asynchronously in my app. I modified that code so the picture saves to the iPhone's local files and can be loaded while offline. I want to make it so this load request times out after a certain interval, possibly 15-20 seconds, and loads the saved file instead of downloading a new one. I found ways to make a web view time out, but Im not sure how to go about doing this using the asynchronous method. How can I make a timeout request for the way that this code loads the url?
Thanks
Edit: I want to make it time out if it is unable to connect to the website and also if the downloading of the picture takes too long.
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:#selector(loadImage)
object:nil];
[queue addOperation:operation];
}
- (void)loadImage {
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.TestURL.com/test.jpg"]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"test.jpg"]];
[imageData writeToFile:localFilePath atomically:YES];
[self performSelectorOnMainThread:#selector(displayImage:) withObject:image waitUntilDone:YES];
}
if you use NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.TestURL.jpg"]]; You are creating a synchronous connection, so You canĀ“t cancel it. You need to wait till the end.
You should implement an asynchronous download using NSURLConnection as explained in How can I deal with connection problem when I use NSData?
Now, I am a newbie at this to, and I realize that this is a way to do it but really doesn't answer the question, but how I handled this situation was to write a method that first checked locally for the image, and if it wasn't there, load it from the web and save it locally, so it was there the next time. Here is some code.
- (UIImage *)checkForLocalImageThenSave:(NSString *)name fromStringURL:(NSString *)url {
NSLog(#"********** Start loading image **********\n\n");
UIImage *image = [[UIImage alloc] init];
NSString *localDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *profilePicName = [NSString stringWithFormat:#"/%#", name];
NSString *profilePicNameOnline = [NSString stringWithFormat:#"%#", url];
NSString *directoryWithProfilePicName = [localDirectory stringByAppendingString:profilePicName];
NSLog(#"Looking for file: %#", directoryWithProfilePicName);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:directoryWithProfilePicName];
if (fileExists) {
NSLog(#"File exists. Load: %#\n\n", directoryWithProfilePicName);
image = [[UIImage alloc] initWithContentsOfFile: directoryWithProfilePicName];
NSLog(#"********** Loading image done **********\n\n");
} else {
NSLog(#"File does not exist. Save: %#", directoryWithProfilePicName);
// TO SAVE A JPEG FILE
NSData *imageWithURL = [[NSData alloc] initWithContentsOfURL:[[NSURL alloc] initWithString:profilePicNameOnline]];
NSLog(#"File at? %#", profilePicNameOnline);
image = [[UIImage alloc] initWithData:imageWithURL];
NSString *jpegFilePath = directoryWithProfilePicName;
NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data writeToFile:jpegFilePath atomically:YES];
NSLog(#"Saving image done.");
}
return image;
}

How to retrieve the set of images from server and store it in documents directory in iOS?

I want to retrieve a folder containing 10 images from server, then store that folder in my document directory. I did some code, but when I run it, I am getting the image urls, not the images themselves. Can anyone help me out?
My code:
-(void)viewWillAppear:(BOOL)animated
{
NSMutableData *receivingData = [[NSMutableData alloc] init];
NSURL *url = [NSURL URLWithString:#"http://Someurl.filesCount.php"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivingData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error = nil;
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
printf("\n the path is :%s",[path UTF8String]);
NSString *zipPath = [path stringByAppendingPathComponent:#"filesCount.php"];
[receivingData writeToFile:zipPath options:0 error:&error];
NSString *documentsDirectoryPath = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"filesCount"];
NSLog(#"the path %#",documentsDirectoryPath);
}
I made this function in my previous project. You need to pass your imageView and serverUrl, then its automatically show image in your imageView and save image to temp directory, when you want again to fetch same image, then next time it take image from disk.
+(void)downloadingServerImageFromUrl:(UIImageView*)imgView AndUrl:(NSString*)strUrl{
NSFileManager *fileManager =[NSFileManager defaultManager];
NSString* theFileName = [NSString stringWithFormat:#"%#.png",[[strUrl lastPathComponent] stringByDeletingPathExtension]];
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"tmp/%#",theFileName]];
imgView.backgroundColor = [UIColor darkGrayColor];
UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[imgView addSubview:actView];
[actView startAnimating];
CGSize boundsSize = imgView.bounds.size;
CGRect frameToCenter = actView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
actView.frame = frameToCenter;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSData *dataFromFile = nil;
NSData *dataFromUrl = nil;
dataFromFile = [fileManager contentsAtPath:fileName];
if(dataFromFile==nil){
dataFromUrl=[[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strUrl]] autorelease];
}
dispatch_sync(dispatch_get_main_queue(), ^{
if(dataFromFile!=nil){
imgView.image = [UIImage imageWithData:dataFromFile];
}else if(dataFromUrl!=nil){
imgView.image = [UIImage imageWithData:dataFromUrl];
// NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"tmp/%#",theFileName]];
BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:dataFromUrl attributes:nil];
if(filecreationSuccess == NO){
NSLog(#"Failed to create the html file");
}
}else{
imgView.image = [UIImage imageNamed:#"NO_Image.png"];
imgView.tag = 105;
}
[actView removeFromSuperview];
[actView release];
});
});
}
Try using this code.
NSURL* url = [NSURL URLWithString:#"http://imageAddress.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
// do whatever you want with directory or store images.
NSImage* image = [[NSImage alloc] initWithData:data];
}
}];
Use this code to download the image using URL and store in document directory. Iterate the logic for set of images to download and store.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imageURL = #"http://sampleUrl";
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"Image1.png"]];
if(image != NULL)
{
//Store the image in Document
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile: imagePath atomically:YES];
}
If you'll showing the images loaded from server on UI, then
try SDWebImageView, can be used with UIButton or UIImageView, very easy and efficient.
example,
[yourImageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Read how to use it?
Okay, now for multiple images, you may need to run a loop, if you've a common base url for all your pictures (on server) something like http://yoururl/server/pictures/1.png will be replace by 2.png 3.png ... n.png, or you get different urls for pictures need to pass that url, you can load it into imageview objects, and later save them into document directory (remember, SDWebImageView by default doing this work for you). You can turn this off too.
P.S. It will load images once and stored into local (in cache) it self, next time when you pass the same image url, it won't load from server and directly load the image from local.

ios store URL images in offline mode(not connected to internet)

I am fetching array of images from different urls..the images are loading perfectly..but if i need to display the images in offline mode..I cant find a right way to get it.Here is my code
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *arr = [NSArray arrayWithObjects:[NSURL URLWithString:
#"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
[NSURL URLWithString:#"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
[NSURL URLWithString:#"http://challenge.roadef.org/2012/img/google_logo.jpg"],
[NSURL URLWithString:#"http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
[NSURL URLWithString:#"http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
[NSURL URLWithString:#"http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
[NSURL URLWithString:#"http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.jpg"],Nil];
NSURL *url=[NSURL URLWithString:arr];
NSData* theData = [NSData dataWithContentsOfURL:url];
int row = 0;
int column = 0;
for(int i = 0; i < [(NSArray*) url count]; ++i) {
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*) [(NSArray*)url objectAtIndex:i]]];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+16, row*90, 80, 80);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:arr];
[theData writeToFile:localFilePath atomically:YES];
}
Guidance please...
Try SDWebImage, SDWebImage can download image and save into disk for cache, that you can use in offline.
You should be saving the images to your device, so you can access them when you're not connected to the internet. The way you do it right now you always load the url.
You can try it like this:
Use a NSData object for every image and load the url content in it.
NSData* theData = [NSData dataWithContentsOfURL:yourURLHere];
Now you can save this object to your device and access it whenever you need to.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"picName.jpg"];
[theData writeToFile:localFilePath atomically:YES];
And that should be about it

Resources