I inserted an image view in my view controller but dont actually know how to display image on it.
I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
id path = #"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *firstImage = [[UIImage alloc] initWithData:data cache:NO];
}
But then it says that 'No visible #interface for UIimage declares the selector initwithData:cache'
I tried adding this line to the .h file but does not work either:
#property (nonatomic, retain) UIImageView *firstImage;
I know that the last step would be to assign that .h create class to the UIimage in the viewcontroller.
You can use try this code to add image from the path in your imageview
NSURL *url = [NSURL URLWithString:#"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
imageView = [[UIImageView alloc] initWithImage:image];
Don't forget to show your imageView as outlet to xib if you are adding through GUI otherwise add it to your view as subview
Try [[UIImage alloc] initWithData:data]; you can cache using an NSCachedURLResponse for the response.
You can use this to load images from URLs:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
But this will block your app until your image loads. It's best to load your images asynchronously when using images from the web. If the images are downloaded in a different thread user can keep interacting with the app. You can check this example from apple about lazy loading images: LazyTableImages
Or you can checkout this tutorial for async image download:
UItables with downloaded images
Related
i have implemented image picker controller delegate:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]) {
UIImage *anImage = [info objectForKey:UIImagePickerControllerOriginalImage];
//i know i can get image name from info key.
if(self.block) self.block(YES, anImage, nil);
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
i am using UIImageView+PlayGIF category to show gif images (loader in my app) from main resource bundle successfully.Some thing like:
NSData *gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"loading_image.gif" ofType:nil]];
CGRect bnd = view.bounds;
loader.gifView = [[YFGIFImageView alloc] initWithFrame:CGRectMake(bnd.size.width/2-60, bnd.size.height/2-70, 120, 120)];
loader.gifView.backgroundColor = [UIColor clearColor];
loader.gifView.gifData = gifData;
Questions:
So how can i export photo library gif to document directory as (whatevername.gif), and use above category to show gif.
Or can i get direct nsdata from photolibrary and use above category to show gif.(i prefer this).
I have not tried with UIWebView, can i show gif on UIWebView.
Thanks
set property of imageView.
#property (strong, nonatomic) IBOutlet UIImageView *YFGIFImageView;
Download the Class of GIF imageLoad with UIImageView
GIFImageLoad
Now load Data from NSBundle.
NSURL *url = [[NSBundle mainBundle] URLForResource:#"loading_image" withExtension:#"gif"];
Load image with animatedImageWithAnimatedGIFData as below.
self.YFGIFImageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
OR
Load image with URL animatedImageWithAnimatedGIFURL as below.
self.YFGIFImageView.image = [UIImage animatedImageWithAnimatedGIFURL:url];
OR
Load image with GIFURL animatedImageWithAnimatedGIFURL as below.
self.YFGIFImageView.image = [UIImage animatedImageWithAnimatedGIFURL:url];
I was trying to put the htttp link to the UIimageView but it doesn't show up me an image. But when i point a local file in the local storage, it shows me the image. Please help me guys.
You should do something like this
__block UIImage *img;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:#"some_link_to_image"];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
yourImageView.image = img;
});
});
You need to download it before displaying. You have different alternatives, you could use a Cocoa API or just an external library to manage the request.
One of the external libraries to handle HTTP requests is AFNetworking
In particular it offers a very convenient category UIImageView+AFNetworking.h You can use it like this:
#import "UIImageView+AFNetworking.h"
//...
NSURL *url = [NSURL URLWithString:#"http://here_your_url_to_the_image"];
[yourImageView setImageWithURL:url];
This question already has answers here:
iOS: load an image from url
(9 answers)
Closed 8 years ago.
I'm trying to load an image from an image URL. All the code I have researched and tried finds a few errors, a lot having to do with new ARC compatibility.
I need the image to load into an image view.
Any help appreciated.
Thanks.
I will just adapt Jim Dovey answer from here Getting Image from URL Objective C :
Synchronous version
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
Asynchronous version
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: data]];
});
});
UIImage *image = [UIImage imageWithContentsOfURL:[NSURL URLWithString:#"http://i.imgur.com/ytxO16A.png"];
I have a UIWebView that showing text and images from web.
I want to do it like when i tap on Image in UIWebView , i want to show it in Full Screen including Orientations.
so i tested with following codes.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
[self presentViewController:vc animated:YES completion:nil];
return NO;
}
else
{
return YES;
}
}
It doesn't work and showing error when i tap image in UIWebView.
2014-03-12 13:59:40.397 MMSP[1368:a0b] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 0; nan nan]'
*** First throw call stack:
Is there anyway to do like i said above?
a) I think you should set the frame for GGFullscreenImageViewController
b) Generally (not having to do with your crash), i think you should not use [[NSData alloc] initWithContentsOfURL:URL]; to load the image at this point. On clicking a link, it will wait until the image has downloaded, and then show it, resulting in a 'lag' for your user.
Instead, just hand over the URL to your GGFullscreenImageViewController, the view controller and have it display an activity indicator until it has downloaded the image.
If you already have a UIImageView setup on your GGFullscreenImageViewController then why not just create the UIImage and assign, instead of creating a new UIImageView. The image view instance in liftedImageView is lost when you assign newly created instance.
Just do this,
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView.image = [UIImage imageWithData:data];
Ideally, you should pass the image url to the GGFullscreenImageViewController by providing initWithUrl: method. Do the downloading of image asynchronously or you could use open source code which does this, ex: SDWebImage.
Hope this helps!
I have a two for loops in my Xcode project which are using to change the image in 9 UIImageViews. The images in these UIImageViews is downloaded from a server and then presented.
My for loops use 3 different integers to figure out what image to show:
next, current_photo and previous are integers.
I have two UIButtons which control showing the next and previous set of images.
When I want to show the next set of images, I made and use the following for loop:
NSArray *imageViews = #[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
for (next = current_photo; next < (current_photo+9); next++) {
NSString *next_set = [NSString stringWithFormat:#"%#%i.%#", SERVER_URL, next+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(next-9)] setImage:image];
}
current_photo = next;
This for loop works perfectly and all 9 UIImagesViews change image.
However, when I want to show the previous set of images in the 9 UIImageViews, the following for loop does NOT work properly for some reason:
NSArray *imageViews = #[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
for (previous = current_photo; previous > (previous-9); previous--) {
NSString *next_set = [NSString stringWithFormat:#"%#%i.%#", SERVER_URL, previous+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(previous-previous)] setImage:image];
}
current_photo = previous;
What is wrong with my for loop? Please explain.
Below is what happens when my app is opened:
Below is what happens when the next button is pressed:
And finally when pressing the back button the app just freezes.....
Why? Whats wrong? Please help.
Thanks for your time :)
For one thing:
[[imageViews objectAtIndex:(previous-previous)] setImage:image];
will always be setting the image at [0] and none of the others will have anything set for them.
Also
(previous = current_photo; previous > (previous-9); previous--)
will loop forever! Every time you do a previous--, the thing you are comparing against to know when to stop, previous-9 also goes down one.
I would recommend this:
for (previous = current_photo; previous > (current_photo-9); previous--) {
NSString *next_set = [NSString stringWithFormat:#"%#%i.%#", SERVER_URL, previous+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(8 - (current_photo - previous))] setImage:image];
}