How can I download images into the app? [duplicate] - ios

This question already has answers here:
iOS download and save image inside app
(11 answers)
Closed 10 years ago.
How can I download images into the app? Like I want to fetch images from my website and download it into the person's iphone app to be shown in the app? Basically the image won't be shown by url.
More specific:
How do I download the image to the app without using UIImage. I want to fetch the image and download it as the file name "anne.png" then reference it throughout the app using UIImage as anne.png. See - I want to download it first so that when someone visits the app a second time, they see the image, and see a default image in the meantime.. Thanks.?

http://mobiledevelopertips.com/cocoa/download-and-create-an-image-from-a-url.html
URL to Remote Image
We start by creating a URL to the remote resource:
NSURL *url = [NSURL URLWithString: #"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
Create UIImage from NSData
The next step is to build a UIImage using the data downloaded from the URL, which consists of an NSData object that holds the remote image contents:
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
Putting it Together
Here’s how to wrap it all together, adding the remote image as a subview to an existing view by creating a UIImageView from the above UIImage:
NSURL *url = [NSURL URLWithString:#"http://mobiledevelopertips.com/images/logo-iphone-dev-tips.png"];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];

For single image you can use the following. Keep in mind this will block the UI till the image is fully downloaded.
[UIImage imageWithData:[NSData dataWithContentsOfURL:photoURL]];
To download the image without blocking the UI:
dispatch_queue_t downloadQueue = dispatch_queue_create(“image downloader”, NULL);
dispatch_async(downloadQueue, ^{
[NSData dataWithContentsOfURL:photoURL];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
// Code to show the image in the UI goes here
});
});
To save the image to the phone camera roll you can use UIImageWriteToSavedPhotosAlbum.
To save the image in the app's directory. Use NSData's writeToFile:atomically:
To download several images from a website maybe this can help you:
What's the best way to download multiple images and display multiple UIImageView?

Related

UIImageView displays downloaded images from HTTP, but won't display image resource

In my first View Controller after a user logs in, there is a quick check to see if the user has uploaded a custom image to his account. (If he has, there's a URL in the user's profile data.)
These images are downloaded correctly in my code and they show up beautifully. The problem comes when there is no custom user image, and the VC tries to set the same UIImageView to a default picture (in xcassets). The picture is being loaded into the bundle, as far as I can tell, and it's a valid PNG file.
Here is the snippet for setting the image. If a custom URL is not found, I set the URL parameter string to "nil."
-(void) setImageWithUrl: (NSString *) Url Imageview: (UIImageView *) image {
if (Url.length > 4) {
NSURL *url = [NSURL URLWithString:Url];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *Profileimage = [UIImage imageWithData:data];
[image setImage:Profileimage];
}
else {
UIImage *Profileimage = [UIImage imageNamed:#"DefaultPicture"];
[image setImage:Profileimage];
}
}
This one's driving me nuts, so all weird ideas are welcome. :-)
Let me know if you want me to post any other parts of the code that you think could be a factor.
It looks like debug-need issue. But why don't you use any image download/cache library, like SDWebImage?
It gonna be much efficient and convenient

How can parse all the images form Website in IOS SDK

I like to implement image upload functionality in my app.For that I like to give some option for user to choose image.
One of the option is choose image from website.
If the user enter an url link in the text box and click the get images button means. I like to display all the images in that particular link
But I am not having any idea about this.
I have explain my requirement below
1)Enter the url in a text box
2)Click the get images button
3)Parse all the images from that particular url
4)Display all the images in the Imageview
And also to illustrate my requirement I have attached the screenshot below
If anybody work around this functionality means please suggest me some idea
Thanks
Like this ?
- (IBAction)buttonClicked:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.yourwebvsite.com/image.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData: data];
UIImageView *displayImageView = [[UIImageView alloc] initWithImage:image];
[displayImageView setFrame:CGRectMake(0, 0, 300, 300)];
[displayImageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:displayImageView];
}
Or if you want all the images from a web directory. See here and save all the images in an array then display the images in a UIImageView like my code above.

https images not showing on UIImageView

I need show the image from https (ex. https://ton.twitter.com/1.1/ton/data/dm/433966760199725056/433966760212332544/du3THdSe.png) by UIImageView in my iOS app. This image opens in browser, but does not show in UIImageView by using these methods:
NSURL *url = [NSURL URLWithString:#"https://ton.twitter.com/1.1/ton/data/dm/433966760199725056/433966760212332544/du3THdSe.png"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
imageView.image = image;
or
// using SDWebImage
[imageView setImageWithURL:[NSURL URLWithString:#"https://ton.twitter.com/1.1/ton/data/dm/433966760199725056/433966760212332544/du3THdSe.png"] placeholderImage:nil];
How can I display this image?
It won't display until you login into twitter with your app. The app can only display the image when you have an open Session with Twitter.
Try calling that URL with your Chrome or Safari in Incognito/Private Mode and see what happens!
You will be redirected to the login page of Twitter. Same thing happens with your app.
Hope that helps.
Cheers,
Sebastian

relative Path for Ipad Project

I know that this is a stupip question, but i cannot find out what my relative Path is... :(
I have an iPad Project and under this a folder for Images
I want to set an UIImage, but i cannot figure out what my relative path is--
I tried this:
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"/Images/send.png"];
But it is not working....
Best regards
You don't have to find the relative path, if your image is present in your bundle. However [[NSBundle mainBundle] bundlePath] will give the relative path of your bundle.
UIImage *image = [UIImage imageNamed:#"send.png"];
This is basic method for loading the image.
UIImage *image = [[UIImage alloc]initWithContentsOfFile:#"send.png"];
This will load the images faster. Because it load the image without cache.
UIImage *image = [UIImage imageNamed:#"send"];
Use this if you want to use the image for both ordinary and retina screens. In your bundle you should have two images "send.png" and "send#2x.png" where "send#2x.png" is twice as the size of "send.png".

Fastest way to load images into a UIView?

i have a UIScrollView which holds different UIView container. Each container has to load six UIImages. When scrolling i need to adjust the contents within the "layoutSubviews" method.
Unfortunately loading these images affects in hiccups when the new container and the images will be created.
I´m loading the images async via "dispatch_async". I also used small 32x32px thumbnails which will be replaced with the full image 256x256px image. Unfortunately its stuttering..
Here´s the code of the "initWithFrame" method of such a UIView.
These UIViews will later be added as a subview to the UIView container.
// load ThumbnailImage and replace it later with the fullImage
NSString* thumbImageName = [self.data.imageName stringByAppendingString:#"_small"];
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: thumbImageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: self.data.imageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
});
});
Is this the faster way to load images into a UIView?
How does Apple implemented the really fluid image view inside the "Photo.app" when scrolling between different fullscreen images?
I don´t know if it would make sense to use CoreImage?
Any advice would be great!
Thanks for any help and your time.
Have a nice day.

Resources