I want to delete a UIButton from a UIScrollView without affecting the rest of the images.
The problem I am having is that one the first request the last image gets deleted (even if that is not the selected one) and then any subsequent requests result in nothing being deleted.
I started off by simply doing if(img == image) but that didn't produce a match. Then after reading this question I tried the second method (below) but that produces two different NSData results for the matching images.
Here is my code:
-(void) deleteImage:(UIButton*)button{ UIImage *image = [button imageForState:UIControlStateNormal]; NSLog(@"image %@", image); for (NSArray *imageArr in self.imageArray) { NSString *filename = imageArr[0]; UIImage *img = imageArr[1]; if([self image:img isEqualTo:image]){ RUN_ON_UI_THREAD(^{ [button removeFromSuperview]; }); double index = [self.imageArray indexOfObject:imageArr]; [self.imageArray removeObjectAtIndex:index]; } NSLog(@"img %@", img); } self.numberOfPhotosLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)[self.imageArray count]]; } - (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2{ NSData *data1 = UIImagePNGRepresentation(image1); NSData *data2 = UIImagePNGRepresentation(image2); return [data1 isEqualToData:data2]; } deleteImage is called when the user holds down on a UIButton.
On viewDidLoad I look through the imageArray and use the following to add the UIButtons to the screen:
-(void) addImageToScreen:(UIImage*) image; { int adjustHeight = 0; int adjustWidth = 10; int imagesInARow = 7; int imageHeight = 75; int imageWidth = 75; int count = [self.imageArray count]; self.numberOfPhotosLabel.text = [NSString stringWithFormat:@"%i",count]; double index = 0; if (count > 0) { for (NSArray *imageArr in self.imageArray) { UIImage *img = imageArr[1]; if(image == img){ index = [self.imageArray indexOfObject:imageArr]; } } } if (count > 1 && count < imagesInARow) { adjustHeight = 0; adjustWidth = (20 * index) + (index * imageWidth); } UIButton* container = [[UIButton alloc] init ]; //create long press gesture recognizer(gestureHandler will be triggered after gesture is detected) UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; //adjust time interval(floating value CFTimeInterval in seconds) [longPressGesture setMinimumPressDuration:1.0]; //add gesture to view you want to listen for it(note that if you want whole view to "listen" for gestures you should add gesture to self.view instead) CGRect frame = CGRectMake(adjustWidth, 5, imageWidth, imageHeight); NSLog(@"self.imageArr %lu", (unsigned long)[self.imageArray indexOfObject:image]); [container setTag:[self.imageArray indexOfObject:image]]; [container setImage:image forState:UIControlStateNormal]; [container setFrame:frame]; [container addTarget:self action:@selector(displayFullScreenImage:) forControlEvents:UIControlEventTouchUpInside]; [container addGestureRecognizer:longPressGesture]; heldButton = container; [self.photosView addSubview:container]; [self.view addSubview:self.photosView]; } Here is how I populate imageArray:
-(void)buildImageArray{ [imageArray removeAllObjects]; NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSArray* dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL]; [dirs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSString *filename = (NSString *)obj; NSString *extension = [[filename pathExtension] lowercaseString]; NSString *fileURL = [documentsDirectory stringByAppendingString: [@"/" stringByAppendingString:filename]]; NSString *pack_id = [NSString stringWithFormat: @"%ld", (long)self.dataObject.pack_id]; NSNumber *newNID = NID; if([NID integerValue] == -1 && [dict1 count] > 0){ int noteID = [[dict1 valueForKey:@"noteID"] integerValue]; newNID = [NSNumber numberWithInt: noteID - 1]; }else if([NID integerValue] == -1){ newNID = [NSNumber numberWithInt:[NID integerValue] - 1]; } NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; //self.signedInSwitch.on = true; NSString *username = [prefs objectForKey:@"username"]; NSString *matchFileName = [NSString stringWithFormat:@"%@_%@_%@_notes_image", username, pack_id, newNID]; if ([fileURL containsString:matchFileName]) { NSLog(@"filename %@", filename); UIImage *image = [UIImage imageWithContentsOfFile:fileURL]; if(image != nil){ NSMutableArray *subArray = [[NSMutableArray alloc] init]; [subArray addObject:filename]; [subArray addObject:image]; [imageArray addObject:subArray]; } } }]; } Here is the response I get from comparing the images:
Selected Image <UIImage: 0x17428e5b0> size {4032, 3024} orientation 0 scale 1.000000 2016-12-02 13:26:38.517130 eCoss[2806:1722107] Array Image 0 <UIImage: 0x17428e970> size {4032, 3024} orientation 0 scale 1.000000 2016-12-02 13:26:39.357693 eCoss[2806:1722107] Selected Image <UIImage: 0x17428e790> size {4032, 3024} orientation 0 scale 1.000000 2016-12-02 13:26:39.357819 eCoss[2806:1722107] Array Image 1 <UIImage: 0x17428e970> size {4032, 3024} orientation 0 scale 1.000000 2016-12-02 13:26:40.318356 eCoss[2806:1722107] Selected Image <UIImage: 0x17428e970> size {4032, 3024} orientation 0 scale 1.000000 2016-12-02 13:26:40.318485 eCoss[2806:1722107] Array Image 2 <UIImage: 0x17428e970> size {4032, 3024} orientation 0 scale 1.000000
image(the selected one) matches one of the images in theimageArrayif([self image:img isEqualTo:image]){never produces a match when it should