I am working on keyboard app,I am trying to show image from url in the extension but code not working though this code is working fine for my iphone app
UIImageView * kanyeGifImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, cell.frame.size.width, cell.frame.size.height)];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"some image url"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
kanyeGifImage.image = [UIImage imageWithData: data];
tempImage = kanyeGifImage.image;
// kanyeGifImage.image = [self squareImageFromImage:cell.appImageView.image scaledToSize:320];
});
});
I'm guessing that the NSURL is not a web url, but a reference to a file in your bundle. Bundles are not by default shared between your main app and your extension. Read up on the section "Sharing Data with your Containing App" https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html
Related
I am working on parsing, I got the image data form the XML like Link
then i sent this to detail view controller by using shared delegate.
AppDelegate *sharedDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
imageview.image=[UIImage imageNamed:sharedDelegate.imagObj];
I got data in sharedDelegate.image but Imageview.image got only the null value.
Please help me.
If you are getting a string, then use this code :
AppDelegate *sharedDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#",sharedDelegate.imagObj]]];
imageview.image = [UIImage imageWithData: imageData]];
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];
}
I am having problem with the loading of content in the app, I see that the data is fetched by the app but the images take lot of time to load, is there any possibility to load images afterwords. The code is below:
NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row];
UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4];
NSString *photoStrn=[dict objectForKey:#"photo"];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSString *u=[NSString stringWithFormat:#"http://%#",photoStrn];
NSURL *imageURL=[NSURL URLWithString:u];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImage *dpImage = [UIImage imageWithData:imageData];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
avatarimage.image = dpImage;
});
If you want more details I will provide :)
You can use GCD for doing this:
dispatch_async(dispatch_get_global_queue(0,0), ^{
for (NSDictionary *dic in discussionArray)
{
NSString *photoStr=[dic objectForKey:#"photo"];
NSString * photoString=[NSString stringWithFormat:#"http://%#",photoStr];
UIImage *dpImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
}
});
Get SDWebImage Here and add that in your project and include
UIImageView+WebCache.h
in class implementation file
UIImageView *imag=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[imag setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[[jsonarray objectAtIndex:indexPath.row]valueForKey:#"imgurl"]]] placeholderImage:[UIImage imageNamed:#"Icon#2x.png"]];
[self.view addSubview:imag];
[imag release];
SDWebImage will be more useful for loading images from URL.
Hope this Helps !!!
James,
With this line,
[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]]
you make a synchronous network call on the main thread. The current thread will hang untile the network call is complete.
The solution would be to do an asynchronous network call. The AFNetworking library provides a great category to load images asynchronously : UIImageView+AFNetworking.