UIImageView: Obtain image file

Discussion in 'iOS Development' started by mic, Jan 6, 2010.

  1. mic

    mic New Member

    Joined:
    Jan 6, 2010
    Messages:
    5
    Likes Received:
    0
    Hi,

    Is it possible to obtain the image file name (a picture is set in the image box) and display its file name in a text field box?
    How do I generate the code?

    By using the code below?
    txt.setText = Image1.image

    Thanks for help.

    Please Register or Log in to view images

  2. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    So, I'm guessing Image1 is your UIImageView and txt is a UILabel/UITextField.
    In Obj-C, the dot syntax accesses or sets the properties of an object "directly" (not really, but that's what it's supposed to look like). So you should either call:
    [OBJC]txt.text = @"Blah"; // property "directly" accessed
    /* OR */
    [txt setText: @"Blah"]; // message explicitly sent[/OBJC]
    Also: UILabel accepts NSStrings, not UIImages. UIImage doesn't have a 'name' property like NSImage, so you'll have to store the image's name somewhere else, e.g. in an instance variable.
  3. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    No. setText is NSString. Image1.image is UIImage.
  4. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    I know that image is a UIImage.
    The code implies setText is an NSString, but that violates Apple's naming conventions.
    If you mean setText as in "the text that is set," I get your point, but it's still misleading.
  5. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    @lauNchD

    The dot syntax invokes the setter/getter methods as specified in an @property declaration. In other words 'myLabel.text = @"Blah";' does not simply retrieve the pointer and assign it, a setter method is invoked, which could involve a copy or retain message. That is the reason many dislike the dot-syntax. (It appears to be something other than it truly is).

    I see that you did say "not really" with regards to the direct access, but I wanted to clarify.
  6. mic

    mic New Member

    Joined:
    Jan 6, 2010
    Messages:
    5
    Likes Received:
    0
    Sorry for confuse.

    Please Register or Log in to view images



    Hi,

    In my application has a image box/UIImageView (named image) and a Label box (named txt). Is it possible to write a code to obtain the image file name and display its name in label box?

    Thanks.

    Please Register or Log in to view images

  7. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    It depends on the origin of the image—is it from a file in the app bundle?

    You could theoretically subclass UIImage, overriding +[imageNamed:], calling super (of course) and then storing the filename as an ivar (as launchd mentioned, I think). That doesn't seem like the best course of action, but it's a start.
  8. mic

    mic New Member

    Joined:
    Jan 6, 2010
    Messages:
    5
    Likes Received:
    0

    Erm.. Do you have an example code because I cannot get view?

    Thanks.

    Please Register or Log in to view images

  9. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I don't have "example code", though the process is quite simple. If you do not know to how create a subclass, I strongly suggest that you take some time off and learn objc.

    Here is some pseudocode:
    [OBJC]
    // overridden method
    + (UIImage *)imageNamed

    Please Register or Log in to view images

    NSString *)str {
    [self setFileName:str];
    return [super imageNamed:str];
    }[/OBJC]

    EDIT: I would explore other options, however.
  10. mic

    mic New Member

    Joined:
    Jan 6, 2010
    Messages:
    5
    Likes Received:
    0

    Thanks for your help. Actually I feel exciting when the first time find this programming and want to create some interesting stuff for fun.
    Do you mind to intro some objective C tutorial or good ebook/book?

    Thanks.

    Please Register or Log in to view images

Share This Page