-2

i am newbie in iOS Development i want to add a image array in to UIScrollView when i write a code for that then it display only last image of array in to UIScrollview.i not understand what is problem please give me Solution

I write a code for that

 for(int index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    self.zoomImage.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width +10.0f, self.zoomScroll.frame.size.height);
    self.zoomImage.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
    [self.zoomImage setUserInteractionEnabled:YES];
    [self.zoomImage setMultipleTouchEnabled:YES];
    [self.zoomImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
    [self.zoomImage setUserInteractionEnabled:TRUE];
    [self.objectarray insertObject:self.zoomImage atIndex:index];
    CGSize scrollViewSize=CGSizeMake(self.zoomScroll.frame.size.width*[self.objectarray count], self.zoomScroll.frame.size.height);
    [self.zoomScroll setContentSize:scrollViewSize];
    [self.zoomScroll addSubview:self.zoomImage];
    [self.zoomScroll addSubview:[self.objectarray objectAtIndex:index]];
}

and when i make a local imageview then it was display for that code is this link shows my old code it is working but when i write a code for this it Display only Last array image please give me Solution for that. i know this question asked many times but i not get solution.

Community
  • 1
  • 1
Ashish Gabani
  • 445
  • 1
  • 5
  • 30
  • zoomImage is your ScrollView?if so what is the value of index? – Anil Santo Nov 11 '14 at 09:36
  • bro i edit my code and here zoomImage is my ImageView and ZoomScroll is my Scrollview i want to add zoomimage into ZoomScroll. – Ashish Gabani Nov 11 '14 at 09:41
  • Instead of using scrollview you use "TableView" and add image to each cell by declaring a array of images. You are doing it in difficult way and the result will be same for both. –  Nov 11 '14 at 09:44
  • where do you allocate the zoomImage(uiimageView)? – Anil Santo Nov 11 '14 at 09:44
  • i allocate zoomimage in my .h file like as @property(retain,nonatomic)IBOutlet UIImageView *zoomImage; and Scrollview also like as @property(retain,nonatomic)IBOutlet UIScrollView *zoomScroll; – Ashish Gabani Nov 11 '14 at 09:45
  • so u have a single imageView in a scrollView and your are changing the frame of tat imageview each time.so it will only show the last image – Anil Santo Nov 11 '14 at 09:49
  • then how i write a code for that? then show all image of my array? – Ashish Gabani Nov 11 '14 at 09:51
  • are you kidding? you are using only _one (1)_ `UIImageView` instance, which you are overriding in your loop... you are not simply newbie to iOS, you are newbie to application development and OOP entirely. – holex Nov 11 '14 at 10:22
  • then #holex give me solution please. – Ashish Gabani Nov 11 '14 at 10:24

2 Answers2

1

You should create new imageView. for(int index=0; index < [self.imagesa count]; index++) {UIImageView *imageView = [[UIImageView alloc] init……]}instead of always using self.zoomImage

mengxiangjian
  • 552
  • 2
  • 8
  • #mengxiangjian it is wrking but i want to zoom that imageview when my zoom button was presed then it size was not extended so i make imageview in my .h file please if you have any solution for extended size of my imageview. – Ashish Gabani Nov 11 '14 at 09:58
0

Can you try this :

    for(int index=0; index < [self.imagesa count]; index++)
    {
        NSDictionary *dict=[self.imagesa objectAtIndex:index];
        NSString *image=[dict valueForKey:@"link"];
        UIImageView img=[[UIImageView alloc] init]; // allocate new object
        img.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width +10.0f, img.frame.size.height);
        img.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
        [img setUserInteractionEnabled:YES];
        [img setMultipleTouchEnabled:YES];
        [img sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
        [img setUserInteractionEnabled:TRUE];
        [self.objectarray insertObject:img atIndex:index];
        [self.zoomScroll addSubview:img];
    }
    CGSize scrollViewSize=CGSizeMake(self.zoomScroll.frame.size.width*[self.objectarray count], self.zoomScroll.frame.size.height);
    [self.zoomScroll setContentSize:scrollViewSize];
Rijo Joseph
  • 1,375
  • 3
  • 17
  • 33
  • #Rijo Joseph it is working i know but i want to extended my imageview Size when zoombutton was pressed then how i extended size of my imageview? i extended my scrollview size but the imageview size is not extended. – Ashish Gabani Nov 11 '14 at 10:01
  • by extend size what exactly you are trying to do?? You want to zoom images inside this scrollview individually? – Rijo Joseph Nov 11 '14 at 10:02
  • #Rijo Joseph i want individually zoom my ScrollView with image when zoom button was pressed i write code for that like as self.zoomScroll.frame=CGRectMake(10, 40, 300, 400); then Scrollview size was extended but imageview remainning same.when i use your code. – Ashish Gabani Nov 11 '14 at 10:04
  • when you click on zoom, does it open a new view with this image so that the user can pinch zoom? – Rijo Joseph Nov 11 '14 at 10:06
  • #Rijo Joseph no i not want to Open a new view because my app already contain many xib and i want them into a single view to show zoom result.and i not open when zoom is pressed i show the zoomed scrollview in my current view. – Ashish Gabani Nov 11 '14 at 10:08
  • try this http://stackoverflow.com/questions/26861520/canceling-a-uilongpressgesture#26861520 and in - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView method return the current imageview image from ur object array.. I am not sure whether this will work – Rijo Joseph Nov 11 '14 at 10:16
  • what this link bro? i not understand. – Ashish Gabani Nov 11 '14 at 10:18
  • sorry wrong link http://stackoverflow.com/questions/16877807/pinch-to-zoom-effect-on-uiimageview-inside-scrollview try this – Rijo Joseph Nov 11 '14 at 10:21