Simple For loop issue - ios

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];
}

Related

Show image from URL in Keyboard App Extension

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

How to load url link to imageview properly and how to manage the image in cache?

NSString *img=[arrImages objectAtIndex:i];
imgScrollView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:img]]];
I have displayed my code. The URL links have been stored into an array and the images are also fetching into uiimage view, but only the first two images are loaded into the imageview while the others didn't load .
arrImages:
inside image viewhttp://localhost:8888/welcome/company/0/images/TV Repair.png
this is the link present in the arrImages i don't know why first two images has been loading.
FOR LOOP:
for(NSDictionary *DicHoleCategories in ArrCategory)
{
StrName=[DicHoleCategories objectForKey:#"image"];
if(StrName!=nil)
{
subimages=[NSString stringWithFormat:LocalImage"%#",StrName];
[DicAllValues setObject:subimages forKey:#"image"];
NSError *error =nil;
}
[arrImages addObject:[DicAllValues objectForKey:#"image"]];
}
Can anyone find the issue and help me please?
Go through the below coding
//url string
NSString *ImageURL = #"yoururl";
//string to data conversion
NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
//set image
imageView.image = [UIImage imageWithData:imageData];
please follow the below code
if(StrName!=nil)
{
subimages=[NSString stringWithFormat:LocalImage"%#",StrName];
[DicAllValues setObject:subimages forKey:#"image"];
NSError *error =nil;
[arrImages addObject:[DicAllValues objectForKey:#"image"]];//add here!
}
Plz check now and tell me if its working or not!

Loading Image from URL iOS [duplicate]

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"];

IOS Application Loading Data From Web

I am trying to load data from web with few simple steps:
NSData *JSONData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"******"]];
NSObject *json = [JSONData objectFromJSONData];
NSArray *arrayOfStreams = [json valueForKeyPath:#"programs"];
NSDictionary *stream = [arrayOfStreams objectAtIndex:0];
NSString *str = [[NSString alloc]initWithString:[stream valueForKey:#"image"]];
NSURL *urlForImage1 = [NSURL URLWithString:str];
NSData *imageData1 = [NSData dataWithContentsOfURL:urlForImage1];
_screenForVideo1.image = [UIImage imageWithData:imageData1];
But the problem is I am doing 30 of this right after my application launches...
I want to load about 5 of them, and than load others. Because when I try to load all of them at the same time, my app is not launching all of them loaded...
Is there any way that I can load first few of them, and wait, and than load others?
As for loading them, you should probably display a spinner, start loading the image in background and then replace the spinner with the image once it’s ready.
- (void) viewDidLoad {
UIActivityIndicator *spinner = …;
[self.view addSubview:spinner];
[self performSelectorInBackground:#selector(startLoadingImage)
withObject:nil];
}
- (void) startLoadingImage {
// You need an autorelease pool since you are running
// in a different thread now.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [UIImage imageNamed:#"foo"];
// All GUI updates have to be done from the main thread.
// We wait for the call to finish so that the pool won’t
// claim the image before imageDidFinishLoading: finishes.
[self performSelectorOnMainThread:#selector(imageDidFinishLoading:)
withObject:image waitUntilDone:YES];
[pool drain];
}
- (void) imageDidFinishLoading: (UIImage*) image {
// fade spinner out
// create UIImageView and fade in
}

"No visible #interface declares the selector" error

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

Resources