2

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Behdad
  • 1,459
  • 3
  • 24
  • 36
  • Will you save the images -for example- in documents directory? what will you do with them? – Ahmad F Dec 25 '16 at 10:39
  • @AhmadF I will save them in a created album in Photos. And then I fetch them all(all photos which are in my created album) and show them. I want to show them with my identifier. – Behdad Dec 25 '16 at 10:49
  • 2
    Maybe http://stackoverflow.com/a/32195744/2442804 is helpful – luk2302 Dec 25 '16 at 10:52

2 Answers2

3

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you. But I want to assign my identifier(my note). I want to read that identifier later. For example: I want assign label to each photo. – Behdad Dec 25 '16 at 16:26
  • Listen to what I'm telling you. You can "assign a label" or any other secondary info _in your own storage_, associated with the photo by the unique identifier. – matt Dec 25 '16 at 16:27
  • *because the unique identifier uniquely identifies it* —beautiful poetry! – vikingosegundo Dec 25 '16 at 17:10
  • 1
    @vikingosegundo I know, I'm so underappreciated... :) – matt Dec 25 '16 at 18:09
  • 1
    Thank you for your great explanation and information. – Behdad Dec 26 '16 at 08:33
  • @matt How can I get the `localIdentifier` from UIImage object? I got it from `accessibilityIdentifier` , but now It doesn't return me any string any more. – Behdad Dec 27 '16 at 15:23
  • A UIImage has no local identifier. You said your plan was to save the image as a _photo_ in the _Photos_ library. Assets in the Photos library have a local identifier. – matt Dec 27 '16 at 15:40
-2

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
pkc456
  • 8,350
  • 38
  • 53
  • 109
  • 1
    How is this persisted with the image? – Abizern Dec 25 '16 at 11:14
  • When user fetch the image, he can assign this `stringProperty`. It works like `tag` property. – pkc456 Dec 25 '16 at 11:15
  • Why not just a new type that has the image and the associated information? – Abizern Dec 25 '16 at 11:17
  • I value your opinion. User can create a new model with two properties (i.e image and associated information). Does my approach faulty? – pkc456 Dec 25 '16 at 11:19
  • 1
    There's no need to drop into the objc runtime. It's overly complex and you lose the type safety provided by Swift. – Abizern Dec 25 '16 at 11:20
  • @pkc456 It doesn't work. It returns nil for the property when It returns the image. – Behdad Dec 25 '16 at 11:24
  • @Abizern I want to save the photo in specified album which is created in the Photos. I can't use a new type, It just accepts JPG file format type. – Behdad Dec 25 '16 at 11:27