Only last image is loading from url in scrollview - objective-c

In my iOS app I have have a lots of view and image and labels are the subviews of the. view. My problem is while loading images from url or remote server only last image is loading from url . Other images are loaded with placeholders. Tried many ways but unable to solve it . My code and screenshot are provided below
catScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width,300)];
catScrollView.contentSize = CGSizeMake(([popularCategoryArray count] * 90)/1.8 + 10, catScrollView.frame.size.height);
[_parentScrollView addSubview:catScrollView];
catScrollView.backgroundColor=UIColorFromRGB(0xE8F7FE);
catScrollView.userInteractionEnabled=YES;
catScrollView.scrollEnabled=YES;
catScrollView.delegate=self;
for(int i =0;i<[popularCategoryArray count];i++)
{
Category* category=[popularCategoryArray objectAtIndex:i];
UIView *catView = [[UIView alloc] init];
if(i>=[popularCategoryArray count]/2+1)
{
catView.frame = CGRectMake(10 + y * 100, 4*10+90+10, 90, 120);
y++;
}
else
{
catView.frame = CGRectMake(10 + x * 100, 10, 90, 120);
x=i;
}
imageCat=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 70, 90)];
imageCat.backgroundColor=[UIColor clearColor];
[imageCat sd_setImageWithURL:[NSURL URLWithString:category.ImageURL]
placeholderImage:[UIImage imageNamed:#"wish_list"]];
[catView addSubview:imageCat];
UILabel *labelCat=[[UILabel alloc]initWithFrame:CGRectMake(0, 95, catView.frame.size.width, 25)];
labelCat.backgroundColor=[UIColor clearColor];
labelCat.text=category.BngTitle;
labelCat.textAlignment=NSTextAlignmentCenter;
labelCat.font=[UIFont fontWithName:#"HelveticaNeue-Thin" size:12.0];
[catView addSubview:labelCat];
catView.backgroundColor=[UIColor clearColor];
[catScrollView addSubview:catView];
}
Screenshot

What is happening is that in UIView+Webcache.m's sd_internalSetImageWithURL method, the following code is executed on every run through your loop.
NSString *validOperationKey = operationKey ?: NSStringFromClass([self class]);
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
The operationKey is always the same because in your default invocation it is not set, so it's possed as nil, and when it's nill it's just the class name. SDWebImage has a very aggressive policy of cancelling all previous requests that way.
Some people believe it only does that for multiple requests of the same URL, but this code does not vary on URL so that may be a bug or not. Your best bet may be to call the sd_internalSetImageWithURL method and set your own operationKey (something with a number in it so that they're all different).

Related

Changing the size of the UIImage

I have the following code
else if([annotation.title isEqualToString:NSLocalizedString(#"TITLE 1",nil)]|| [annotation.title isEqualToString:NSLocalizedString(#"TITLE 2",nil)])
{
static NSString *identifier = #"currentLocation";
SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(pulsingView == nil) {
pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pulsingView.annotationColor = [UIColor colorWithRed:0 green:0.678431 blue:0 alpha:1];
pulsingView.image = [UIImage imageNamed:#"image.png"];
pulsingView.frame = CGRectMake(0, 0, 15, 15);
pulsingView.canShowCallout = YES;
}
return pulsingView;
Basically it's an image and besides it we have a pulsing round annotation.
When the image is tapped - the annotation description with either "Title 1" or "Title 2" comes up depending on the conditions (which are not relevant here).
The issue is simple - when i apply
pulsingView.frame = CGRectMake(0, 0, 15, 15);
The image on top of the pulsing view doesn't change size.
Need help with alternative solutions.
Thank you so much guys!
1) Try this
[pulsingView setFrame: CGRectMake(0, 0, 15, 15)];
instead of
pulsingView.frame = CGRectMake(0, 0, 15, 15);
2) If the first solution doesn't work maybe you need to disable the autolayout if you are using Storyboard or...
3) if you use XIB maybe you need to add
[pulsingView setNeedsDisplay];
If previous answer doesn't work, a little trick that I've used sometimes is calling setTranslatesAutoresizingMaskIntoConstraints:.
Your code would look like:
[pulsingView setTranslatesAutoresizingMaskIntoConstraints:YES];

iOS: Programmatically create labels inside scroll view created programmatically

I am developing a project in which I have created scroll view and labels inside scroll view programmatically using following code:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 240)];
int bx=0;
for (NSString * str1 in arr1)
{
if ([arr1 count]<=2) {
label=[[UILabel alloc]initWithFrame:CGRectMake(bx, -1, scrollView.frame.size.width/[arr1 count], 100)];
label.textAlignment=NSTextAlignmentCenter;
}
else
{
label=[[UILabel alloc]initWithFrame:CGRectMake(bx, -1, 106, 100)];
label.textAlignment=NSTextAlignmentCenter;
}
[label setText:[NSString stringWithFormat:#"(%#)",str1]];
[scrollView addSubview:label];
bx +=label.frame.size.width;
}
I want to create another line of labels below the above created labels using new array.and also I want to give different color to each label.
I don't know if I really understood, but maybe you should try to play with the second property of your CGRectMake which is for the Y value.

Subclass of UIPageControl refresh only after uiscrollview move, not before

the problem I've met today is with my subclass of UIPageControl. When I initialize it, the frame (specifically the origin) and image of dots stays default, which is the problem, since I want it to change right after initialization. However, when I move with scrollView (as in "touch and move") after initialization, they (the dots) somehow jump to the right position with correct images.
What could be the problem?
Code:
CustomPageControl.m
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
activeImage = [UIImage imageNamed:#"doton.png"];
inactiveImage = [UIImage imageNamed:#"dotoff.png"];
return self;
}
- (void) updateDots
{
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView *dot = [self.subviews objectAtIndex:i];
if (i == self.currentPage) dot.image = activeImage;
else dot.image = inactiveImage;
[dot setFrame:CGRectMake(i * 13.5, 1.5, 17, 17)];
}
}
- (void)setCurrentPage:(NSInteger)currentPage
{
[super setCurrentPage:currentPage];
[self updateDots];
}
#end
ChoosingView.m - init part
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 160, 300)];
[scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setDelaysContentTouches:NO];
[scrollView setCanCancelContentTouches:YES];
[scrollView setClipsToBounds:NO];
[scrollView setScrollEnabled:YES];
[scrollView setPagingEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
pageControl = [[CustomPageControl alloc] initWithFrame:CGRectMake(200, 300, 80, 20)];
[pageControl setBackgroundColor:[UIColor clearColor]];
pageControl.numberOfPages = 6;
[pageControl setCurrentPage:0];
the last line is when I would expect the UIPageControl to refresh, however that does not happen.
Does this happen with the standard UIPageControl implementation?
Your problem states that your objects subViews (eg the UIImageViews) rects/size are not initialising to your desired size/position.
I implemented this code in my project with a nib rather than programmatically and I needed to call -(void)updateDots to set it as its initial condition was the standard dots..
I dont see how the UIScrollView has any bearing impact on this unless somehow its linked to your -(void)updateDots function (E.g. your setting the currentIndex of your custom page control). You state, "However, when I move with scrollView (as in "touch and move") after initialization, they (the dots) somehow jump to the right position with correct images."
Because they "jump to the right position with correct images" it means that your -(void)updateDots function must be getting called. I dont see any other explanation.
Also your iteration loop assumes that all the UIViews in your .subViews array are UIImageViews, although fairly safe to assume this, I would check to see if the UIView is a UIImageView with reflection.

Identifying the name of a class during run time

So I have run into a fairly complex problem with my application.
I have about 40 UIImageView objects that need to be displayed at different locations when different ones are stacked (EX img 1 needs to take img 2 place if img2 ==NO).
What I am trying to do is use a "for loop" and run through all my Boolean values that check if the image is selected.
I was curious if it is at all possible to call variable like this
-(IBAction) button:(id) sender{
scrollView.hidden = YES;
NSInteger i = 1;
(#"imagenumber%d", i) = [[UIImageView alloc] initWithFrame: CGRectMake (28, 230, 86, 26)];
[[imagenumber(#"%d", i)] setImage:[UIImage imageNamed:#"csa.jpg"]];
[self.view addSubview: [imagenumber(#"%d", i)]];}
}
where #"imagenumber%d" is my NSImageView and i is a number ranging from 1-40.
I'm sorry if this is confusing, I really would appreciate any help at all on the subject because I can’t seem to get any idea and have been working on this problem for several weeks.
Put the views in a NSMutableArray - you can access elements in those by index, use fast enumeration, ... E.g.:
UIImageView *image = [[UIImageView alloc] ...
[myMutableArray addObject:image];
// ...
For the sake of hilarity you could also put them in a intrinsic array and use pointer arithmetic. (Which is probably what happens behind the scenes with faster enumeration for those interested)
UIImageView * imageArray[40];
...
for(UIImageView * this = imageArray; this; this++)
{
this = [[UIImageView alloc] initWithFrame: CGRectMake (28, 230, 86, 26)];
...
}

Having issues removing UIViews from a UIScrollView

I'm trying to make code that will add players to a view based on objects but, I'm having some issues.
Right now currently if I run the initView method with 4 confirmed working Player objects in playerList, only 3 UIViews will appear in my scrollview, then when I click the clear players button, only the last (3rd) UIView will be removed.
A side note, none of my custom buttons appear in the UIViews either, I have an image that they should load with, but its not working.
Thanks in advance for any help.
- (void)clearPlayers {
for (Player* i in self.playerList) {
[i.viewPane removeFromSuperview];
}
[self.playerList removeAllObjects];
}
- (void)initView {
int Loc = 0;
int Count = 1;
int margin = 5;
int height = 100;
for (Player *p in playerList) {
UIView *playerView = [[UIView alloc] initWithFrame:CGRectMake(0, Loc, 320.0, height)];
p.viewPane = playerView;
[playerView setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:playerView];
UIButton *plus = [[UIButton alloc] initWithFrame:CGRectMake(200, 10, (height - 5), (height - 5))];
UIImage *buttonImage =[[UIImage alloc] initWithContentsOfFile:#"Metal_Plus_Up_2.png"];
[plus setBackgroundImage:buttonImage forState:UIControlStateNormal];
[p.viewPane addSubview:plus];
[plus release];
[playerView release];
Loc = Loc + (height + margin);
Count = Count + 1;
}
[scrollView setContentSize:CGSizeMake(320.0, (height * Count) + (margin * Count))];
}
Question: have you verified that the UIImage returned from initWithContentsOfFile is not nil? You might need the full path instead of just the filename
As far as the wackiness with the UIViews not getting removed goes, everything you've posted looks fine as far as I can see. The only thing I can think of is that maybe you don't have retain specified as an attribute for your viewPane property...