views:

720

answers:

2

I have UIScrollView which has subviews (pictures) added do it. each time a user touches the a picture in the scrollview, it toggles a checkmark ontop of it.

NSMutableIndexSet *picturesArray; <- declared in .h

(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {

if (!self.dragging) {
 [self.nextResponder touchesEnded: touches withEvent:event]; 
 NSLog(@"Touch down");
 for (UITouch *touch in touches) {

  for (int i = 1; i <= [self subviews].count; i++)
  {


  if(CGRectContainsPoint([[self viewWithTag:i]frame], [touch locationInView:self])){  
   NSLog(@"touched %d th view",i);

   NSArray *subviews = [[self viewWithTag:i] subviews];
   UIImageView *view = nil;

   view = [subviews objectAtIndex:0];

    if(view.hidden){
     // add the index
     [picturesArray addIndex:i]; 
     view.hidden = NO; //check mark is shown
    }else{
     [picturesArray removeIndex:i]; 
     view.hidden = YES; //check mark is not shown
    }



   // UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); <- WORKS IF CALLED

   }

  } 
 }

}

Question 1: is this the best way of doing this? It seems like using a for (int i = 1; i <= [self subviews].count; i++) is pretty slow. I basically need to capture which subview was touched. I havent figured this out other than going through EACH subview

savePhotos is called and basically searches through which of the pictures was touched and saves them to the Photo Album. However the call to UIImageWriteToSavedPhotosAlbum fails. This is in the same file as TouchesEnded. But when called in TouchesEnded, it works.

  • (IBAction) savePhotos: (id) sender{

    NSLog(@"The index set is %@",picturesArray );

    const NSUInteger arrayCount = picturesArray.count;

    NSUInteger *theIndexBuffer = (NSUInteger *)calloc(picturesArray.count, sizeof(NSUInteger)); UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:0]image], nil, nil, nil);

    [picturesArray getIndexes:theIndexBuffer maxCount:arrayCount inIndexRange:nil];

    for(int i = 0; i < arrayCount; i ++){

    NSLog(@"Element is %d",theIndexBuffer[i]);  
       UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); <- THIS CRASHES
    

    }

}

Question 2: Why is it that UIImageWriteToSavedPhotosAlbum is failing ?

Thank you in advance

+2  A: 

1) instead of using UIImageView, implement a child of UIImageView. Then try listening for touches on the individual subviews, that should solve your O(n) problem

2) something is probably getting auto-released, double check your reference counting is correct

slf
Thanks for your response SLF!Could you elborate on what you mean by implmenting a child of UiImageView?For the autoRelease Issue [(UIImageView *)[self viewWithTag:i]image] is basically NULL. So self viewwithTag:i is being auto released?
A: 
  1. try UIView* targetView = [self hitTest:location withEvent:nil];
Roger Nolan