0

I have a problem with deleting images from my app. I have an array of string, that are images converted to base64 string. So I get back array from API back to my app, and I'm stuck when I want to delete one picture that user has select.

I tried to delete with filter and map method but didn't solve the problem. Here is my "try" "

func deleteImage(image : UIImageView) {
    for img in newAdedImages {
        newAdedImages = newAdedImages.filter({$0 !== image})
        newAdedImages.append(img)
    }
}
  • match your image id and remove from the array – Nirav Kotecha Jul 22 '19 at 12:52
  • 5
    Your code makes no sense at all. You are filtering the (string) array with an `UIImageView` instance, both types are not related. If the strings are base64 strings you have to map them to `Data` and check them against the data representation of the `image` of the image view. – vadian Jul 22 '19 at 12:57

2 Answers2

0

try === operator or add some property to identify your image, also all UIView subclasses have .tag property which can be used as identifier

upd: if you are trying to compare base64 string with UIImageView then seems like u doing something wrong, its better to store UIImageView instead of base64 strings. Imagine your app in abstractions, all the "UI/visual" is view abstraction and the "data" (e.g. base64 strings coming from server) is **data abstraction so u shouldnt mix them up. I dont know the context of your task or so, but there is some pointers I can give:

1) get base64 strings from service/API/etc. (this is data abstraction)

2) use some helper (e.g. some swift class with function) to make UIImage (there goes view abstraction)

3) operate your uiviews as u wish

But this is very simple explanation, I hardly recommend to read more about architecture patterns such as mvvm for example

hh3n7a1
  • 91
  • 8
  • did you read the question? It reads " I have an array of string, that are images converted to base64 string." – Andrey Chernukha Jul 22 '19 at 13:27
  • The update is still bad. One should not store UIImageViews, how do you even imagine this? And why is it better? One should maintain a model object entity which would represent an image and have an actual image (in any form) and its id. All the comparison operations should be performed over these model objects, not ui objects – Andrey Chernukha Jul 22 '19 at 13:44
  • @AndreyChernukha indisputably u should maintain the model, handle uiimage loads in background, cache them and so, thats why I made the point that this is very simple explanation of the main flaws – hh3n7a1 Jul 22 '19 at 13:49
  • so what did you mean by saying "its better to store UIImageView"? This is not a "simple explanation", this is just wrong – Andrey Chernukha Jul 22 '19 at 13:52
  • @AndreyChernukha in terms of subj code I didnt say anything wrong, what he tries to do is compare string with UIImageView – hh3n7a1 Jul 22 '19 at 13:55
  • and this is why you're suggesting storing UIImageView? What do you even mean by saying this? Also, you wrote "use some helper (e.g. some swift class with function) to make UIImage (there goes view abstraction)" which is also wrong. This operation should belong to the model layer – Andrey Chernukha Jul 22 '19 at 14:00
  • @AndreyChernukha this is the way he can compare 2 objs – hh3n7a1 Jul 22 '19 at 15:17
0

Bad code

As @vadian already pointed out code you've posted doesn't make sense because you are trying to filter array of strings with instance of UIImageView. Also you are adding string to array which already contains that string that means you will have a lot of duplicates.

Possible solution

You could check how base64 string is used to create UIImage that is used in UIImageView then you can try to reverse process and extract base64 string from UIImage. Then you can filter array of newAddedImages by comparing string values.

Check this answer on SO: https://stackoverflow.com/a/47610733/4949050

Najdan Tomić
  • 2,071
  • 16
  • 25