Behdad
Behdad

Reputation: 1629

Adding an identifier to UIImage object Swift

I am developing an app that capture and save the photos into the specified photo album. I want to assign an unique identifier to each photo. For example : "pic2;4" . I want to do this, because when I want to retrieve the photos,I want to show some specified information about each photo.

How can I assign a note or something like that to UIImage object?

Upvotes: 2

Views: 3447

Answers (2)

matt
matt

Reputation: 535027

I am developing an app that capture and save the photos into the specified photo album. I want to assign an unique identifier to each photo

You don't need to assign a unique identifier to the photo. After you have saved a photo into the user's Photo library, it has a unique identifier, assigned by the runtime (its localIdentifier). So all you need to do is retrieve that unique identifier and store it off somewhere. You will be able to retrieve the photo using this identifier at any time subsequently. And you can associate any secondary info with this photo, in your own data model, because the unique identifier uniquely identifies it.

Upvotes: 3

pkc456
pkc456

Reputation: 8506

Create Extension of NSObject

import Foundation
import ObjectiveC

// Declare a global var to produce a unique address as the assoc object handle
var AssociatedObjectHandle: UInt8 = 0


extension NSObject {
    var stringProperty:String {
        get {
            return objc_getAssociatedObject(self, &AssociatedObjectHandle) as? String ?? ""
        }
        set {
            objc_setAssociatedObject(self, &AssociatedObjectHandle, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

Set the stringProperty of any object(UIImage object in your case) like below:-

    imageName.stringProperty = "pic24"

Fetch the stringProperty as below:-

let stringTagOfImage = imageName.stringProperty

Upvotes: -2

Related Questions