ios - 比较两个 UIImages Objective-c

标签 ios objective-c uibutton uiimage

我想从 UIScrollView 中删除 UIButton,而不影响其余图像。

我遇到的问题是,第一个请求会删除最后一个图像(即使不是选定的图像),然后任何后续请求都不会删除任何内容。

我一开始只是简单地执行了 if(img == image) 但这并没有产生匹配。然后读完 this我尝试了第二种方法(如下),但它为匹配图像生成了两个不同的 NSData 结果。

这是我的代码:

-(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];
}
当用户按住 UIButton 时,将调用

deleteImage

viewDidLoad 上,我查看 imageArray 并使用以下代码将 UIButton 添加到屏幕:

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

}

这是我填充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];

        }

    }
}];
}

这是我通过比较图像得到的响应:

    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

最佳答案

对于此功能,比较图像数据会使用过多的处理器。相反,您可以在填充 ScrollView 时设置button.tag = [image_index]。

然后,在您的操作中,您可以使用button.tag来识别单击了哪个图像。

- (void)deleteImage:(UIButton*)button { 
    int clickedImageIndex = button.tag;
    ...
    [self.imageArray removeObjectAtIndex:clickedImageIndex];
    ...
}

关于ios - 比较两个 UIImages Objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40932839/

相关文章:

ios - 自定义大小 UIModalPresentationFormSheet 在键盘显示时返回默认大小

ios - UIButton:将背景图像设置为禁用状态会更改默认状态的显示

iOS 10 推送通知本地化

iphone - 如何使用 AFNetworking 中的 enqueueBatchOfHTTPRequestOperations 在操作失败时重新添加操作

ios - Xcode - 如何使用运行脚本将应用程序移出沙箱?

ios - 附加 NSSting 后保留 textView 文本的颜色

iphone - 如何更好地拖动 UIButton?未调用 UIButton UIControlEventTouchUpInside 事件

iphone - 在 iPhone 上检索当前本地时间?

ios - 自定义 UITableViewCell accessoryView 按钮在真实设备上不可见

ios - 如何在循环中设置 UIButtons 的 Action ?