How to download & display an image from ftp server - iphone

(new to xCode and programming) I am trying to connect/download/save to variable and display an image in UIImageView from an ftp server. The code is below. Any help advice would be appreciated.
- (void)viewDidLoad
{
//sets label to notify user whats happening
NSMutableString *labelString;
labelString = [NSMutableString stringWithString: #"Connecting"];
[labelDymamic setText: labelString];
//variable for ftp location
NSString *ftpLocation = [NSString stringWithFormat:#"ftp2.bom.gov.au/anon/sample/gms/IDE00003.201011170430.gif"];
//variable to recieve data
NSMutableData *responseData;
//loads ftpLocation into url
NSURL *url = [NSURL URLWithString:ftpLocation];
//sets label to notify user whats happening
NSMutableString *labelStringDownloading;
labelStringDownloading = [NSMutableString stringWithString: #"Downloading"];
[labelDymamic setText: labelStringDownloading];
//Connect to ftp
self.ftpImageView.image = [UIImage imageNamed:#"Icon.png"];
self.labelDymamic.text = #"Receiving";
NSURLRequest *request = [NSURLRequest requestWithURL:url];
(void) [[NSURLConnection alloc] initWithRequest: request delegate:self];
//download image
//save to variable
//display to screen
//sets label to notify application loading complete
NSMutableString *labelLoadedString;
labelLoadedString = [NSMutableString stringWithString: #"Radar"];
[labelDymamic setText: labelLoadedString];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
At the moment all that is displayed is a white screen with labelLoadedString variable "Radar".
Thanks
Allan

your url is wrong, its missing the SCHEME: ftp://
as for downloading:
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//download
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * r, NSData * d, NSError * e) {
//save
UIImage *img = [[UIImage alloc] initWithData:d];
//display
self.imageView.image = img;
//sets label to notify application loading complete
NSMutableString *labelLoadedString;
labelLoadedString = [NSMutableString stringWithString: #"Radar"];
[labelDymamic setText: labelLoadedString];
}];

After fixing your URL, which as mentioned appears to be incorrect, checkout AFNetworking (see https://github.com/AFNetworking/AFNetworking). There's a category on UIImage that will make your life a lot easier when dealing with loading images from remote locations (such as over http, https, ftp, you name it ;)
Here's an example from their overview:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:#"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder-avatar"]];

Related

Image does not load in UIWebView in iphone

I have UIWebView i want to load image in that but it does not show image in it i am using following code
pdfView.hidden=NO;
webView.hidden=NO;
pdfView.backgroundColor=[UIColor clearColor];
doneButton = [[UIBarButtonItem alloc]initWithTitle:#" Done " style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonClick)];
self.navigationItem.leftBarButtonItem =doneButton;
webView.backgroundColor=[UIColor clearColor];
NSLog(#"File Name is %#",fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"png"];
NSURL *url = [NSURL fileURLWithPath:fileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
is there any way to load this image which is in fileName which comes from sever in this webView thanks
The other way i can go for is to html-page dynamically and load it into UIWebView:
NSString *imageUrlString = #"your url ";
NSString *yourText = #"Some text here";
NSString *htmlString = [NSString stringWithFormat:#"<img src='%#'/><br><b>%#</b>", imageUrlString, yourText];
[myWebView loadHTMLString:htmlString baseURL:nil];
I think you can tighten this up a bit. Instead of getting a string path and creating a file URL from it, just ask the bundle for a URL.
My best guess is your filename already has the file extension in it and you're not actually getting back a valid file path/url. Step through in the debugger and see.
// get the file resource URL
NSURL *url = [[NSBundle mainBundle] URLForResource: filename withExtension: #"png"];
// sanity check - did we get a valid URL?
NSAssert( url != nil && [url checkResourceIsReachableAndReturnError: NULL], #"oops - no resource!");
// load it
[webView loadRequest: [NSURLRequest requestWithURL:url]];
Why can't you just use a UIImageView? This is done by hand but something like;
NSURL *url = [NSURL fileURLWithPath:fileName];
UIImage *tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
imageView.image = tempImage;

How to display multiple image in thumbnail and Not able to play audio in iphone

I am new to iOS programming. I'm fetching images and audios file from server. After that, using below code, I am trying to display all images as a thumbnail. Can any one tell me how can I display all images as a thumbnail. Also audio is not playing.
-(IBAction)fullsizeimage:(id)sender
{
////////////// Image ////////////////////////////
NSURL *url = [NSURL URLWithString:#"http://182.73.152.59:82/php/tauky_services/codeigniter-restserver-master/uploads/45/thumbnail_small/326.png"];
NSData *data = [NSData dataWithContentsOfURL: url];
UIImage *image = [UIImage imageWithData:data];
imagee = [[UIImageView alloc] initWithImage: image];
[self.view addSubview:imagee];
NSURL *url1 = [NSURL URLWithString:#"http://182.73.152.59:82/php/tauky_services/codeigniter-restserver-master/uploads/45/thumbnail_small/327.png"];
NSData *data1 = [NSData dataWithContentsOfURL: url1];
UIImage *image1 = [UIImage imageWithData:data1];
imagee1 = [[UIImageView alloc] initWithImage: image1];
[self.view addSubview:imagee1];
NSURL *urll = [NSURL URLWithString:#"http://182.73.152.59:82/php/tauky_services/codeigniter-restserver-master/uploads/45/thumbnail_small/325.png"];
NSData *dataa = [NSData dataWithContentsOfURL: urll];
UIImage *imageee = [UIImage imageWithData:dataa];
imagee2 = [[UIImageView alloc] initWithImage: imageee];
[self.view addSubview:imagee2];
/////////////// Audio ///////////////////////
NSURL *audiourl = [NSURL URLWithString:#"http://182.73.152.59:82/php/tauky_services/codeigniter-restserver-master/uploads/45/Audio/312.mp3"];
NSData *audioData = [NSData dataWithContentsOfURL:audiourl];
NSError *error;
//AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioData error:&error];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
Thanks
Aslam
Consider assigning a different frame to each image before you add them to the superview. It's possible that your code assigns the same frame for each image.
Use NSLog(#"%#", [self.view subviews]); to know if the image views are present and loaded.
Set different UIImageView (with your required thumbnail frame) on your main view and then add these image's on it.
You can cosider using a GridView from the moriarty library, either as a large grid in a UIScrollView, or on a more efficient row-by-row basis in a UITableView.
You could use TTPhotoViewController from the
Three20 project, which works as iOS built in camera roll to view images.

how to load url image in image view in iphone

- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.scri8e.com/effects/FL0WERZ/PNG/ir09.png"];
data = [[NSData alloc]initWithContentsOfURL:url ];
UIImage *img = [[UIImage alloc]initWithData:data ];
imageview.image=img;
}
above is my code but it does not load the image in image view
Try this library it will help you download images and display in the imageview.
The same code working for me fine !!!!!!
Please check the Internet Connection.
Please check the NSData *data size
Please check the UIImageView Size
Please check it hided by some other controls
Please check it added properly to your View
Please check its Content View
#property(nonatomic,retain) IBOutlet UIImageView *imageview;
NSURL *url = [NSURL URLWithString:#"http://www.scri8e.com/effects/FL0WERZ/PNG/ir09.png"];
NSData *data = [[NSData alloc]initWithContentsOfURL:url ];
UIImage *img = [[UIImage alloc]initWithData:data ];
self.imageview.image=img;
If you are taken imageview directly in xib, than just check connection in connection inspector.
NSString *mainimg=[NSString stringWithFormat:#"%#",[[xmlDataDictionary valueForKeyPath:#"eg.main.img1"]objectAtIndex:indexPath.row]];
NSURL *url = [NSURL URLWithString:mainimg];
NSData *image = [[NSData alloc] initWithContentsOfURL:url];
cell.img.image=[UIImage imageWithData:image];
Try this for parsed url..
So simple..
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *url_Img1 = #"http://www.scri8e.com/effects/FL0WERZ/PNG";
NSString *url_Img2 = #"ir09.png";
NSString *url_Img_FULL = [url_Img1 stringByAppendingPathComponent:url_Img2];
NSLog(#"Show url_Img_FULL: %#",url_Img_FULL);
_view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img_FULL]]];
}
use EgoImageView instead of ImageView.

Get image from web to UIImageView don't works

I have a big problem with very simple code.
I need to get a picture from a Facebook URL and put it on a UIImageView.
I don't know but don't works.
I have tried different links also (no Facebook links also don't works).
-(IBAction)setUserPhoto:(id)sender{
NSURL *url = [url initWithString:#"http://graph.facebook.com/100000769380612/picture?type=large"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
if(image){
NSLog(#"Image OK");
} else if (!image) {
NSLog(#"Error");
}
[userPhoto setImage:image];
}
Thank you.
The string has a substitution variable in it, that hasn't been substituted:
http://graph.facebook.com/%#/picture?type=large
You are missing the alias (thats the error i get if i put this url into safari)
Use
NSString *urlString = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?type=large", replacement]; //where replacement is the string that is supposed to go there
NSUrl *url = [NSUrl URLWithString:urlString];
Try the url in the web browser first, to ensure it exists.
You are missing something in url..it contain %# , please replace it with proper string
for eg :
https://graph.facebook.com/DoloresPark/picture?type=large
you URL should be some thing like the above one
-(IBAction)setUserPhoto:(id)sender{
NSString *user = #"DoloresPark";
NSString *urlString = [NSString
stringWithFormat:
#"http://graph.facebook.com/%#/picture?type=large",user];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
if(image){
NSLog(#"Image OK");
} else if (!image) {
NSLog(#"Error");
}
[userPhoto setImage:image];
}
have you tried dataWithContentsOfURL:options:error: and reading the &error for any pointers?
The problem is that you are not making a connection to the URL.Use the following -
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:#"Your URL"]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
your class should implement the NSURLConnectionDelegate -
Use the following delegate methods to get the data -
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
Hope it helps!

help needed with adding picture from url!

i've managed to incorporate the twitter api into my app and i can call all the data and put it into my app, except for the image.
i've managed to find tuts on how to get the picture if you provide the url, but i want to get the addy from my dictionary to then be put into my uiimageview.
Any help would be appreciated, here is what i have sourced
NSData *imageData =
[[NSData alloc] initWithContentsOfUrl:
[NSString
stringWithContentsOfUrl:
[NSURL URLWithString:#"http://mydomain.com"]
encoding: NSUTF8StringEncoding
error: nil
]
];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[marcaBackground setImage:image];
[imageData release];
[image release];
this is what i have already set up`-(NSString*)author {
return [[contents objectForKey:#"user"] objectForKey:#"screen_name"];
`
NSString *author=[(Tweet*)[auth objectAtIndex:indexPath.row] author];
could someone please advise me on what needs changing for it to work??
NSData *imageData = [[NSData alloc] initWithContentsOfUrl: [NSURL URLWithString:#"http://..."]];
The rest is correct.