Uiimageview - How to Get the File Name of the Image Assigned

UIImageView - How to get the file name of the image assigned?

Nope. You can't do that.

The reason is that a UIImageView instance does not store an image file. It stores a displays a UIImage instance. When you make an image from a file, you do something like this:

UIImage *picture = [UIImage imageNamed:@"myFile.png"];

Once this is done, there is no longer any reference to the filename. The UIImage instance contains the data, regardless of where it got it. Thus, the UIImageView couldn't possibly know the filename.

Also, even if you could, you would never get filename info from a view. That breaks MVC.

How to get Image Name used in an UIImage?

That functionality is not built-in to UIImage because images are not always loaded from files. However, you could create a custom UIImageView subclass to fit your needs.

swift, How to get currently displayed image file name from UIImageView

You can't do this. Neither in swift nor objective-c.

The thing to do is to store the data you want to retrieve. That is... store the name somewhere and use that to load the image. Not the other way around.

So create a property something like imageName and then use that to load the image.

How do you get image file name from UIImagePickerController?

I found a way

if let asset = info["UIImagePickerControllerPHAsset"] as? PHAsset{
if let fileName = asset.value(forKey: "filename") as? String{
print(fileName)
}
}

This will return you the actual file name in the photo library.

how to get image name when click on uiimageview?

UIImageView just keeps the pointer to the place the UIImage is stored inside the memory, so it's not possible with UIImageView. You would need to create a subclass of UIImageView with a NSString variable which stores the value to access it later. Something like this:

#import <UIKit/UIKit.h>

@interface MyImageView : UIImageView

@property (strong, nonatomic) NSString *imageName;

@end

Get Button Image File Name in Swift

You can reuse the image using your first line:

let image = sender.currentImage

Appium get image file name of XCUIElementTypeImage

While it would be a very dirty way to write your view, you could theoretically pass the image's filename as the accessibility ID. As you need to still identify the element you would still use the accessibility label to describe the element.

Why is this "dirty"? Your UIImageView contains an instance UIImage, not a file. The UIImage also does not store the filename, only the file's data. For this reason you'd have to get really messy about passing that filename around. This breaks a lot of view controller pattern rules.

This is not a good candidate for a UI test. Or if it is, not with Appium. Or if it is, with some sort of screenshot comparison tool (I've been out of Appium for a few years, but I'm sure you could make even an external tool work with enough effort).

Again, this seems like a lot of effort and that's always a red flag to me.



Related Topics



Leave a reply



Submit