Start a new topic
Answered

Fetch property with type UIImage returns NSDictionary

I have property UIImage

h. file 

@property (nonatomic, copy) UIImage *exampleImage;


m.file

- (NSDictionary *)hostToKinveyPropertyMapping{ 

 return @{ 

 @"exampletId" : KCSEntityKeyId, 

 @"exampleImage" : @"image"

 };

}


+(NSDictionary *)  kinveyPropertyToCollectionMapping

{

return @{@"coverage":KCSFileStoreCollectionName};

}


Can I fetch from backend UIImage instead of NSDictionary for property exampleImage?


Best Answer

Wood,


Just trying to summarize the steps for uploading images:



Step 1: Define the class :

    class Image : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable
        var entityId: String! //Kinvey entity _id
        var image: UIImage!
    }

Step 2: Override the hostToKinveyPropertyMapping & kinveyPropertyToCollectionMapping method:
   
        override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
            return [
                "entityId" : KCSEntityKeyId, //the required _id field
                "image" : "image"
            ]
        }
       
        override class func kinveyPropertyToCollectionMapping() -> [NSObject : AnyObject]! {
           
            return [
                "image" : KCSFileStoreCollectionName
            ]
        }
   
Step 3: Initialize store object with KCSLinkedAppDataStore object.

        var collection = KCSCollection(fromString: "Images", ofClass:Image.self)
       
        var imageToSend = KCSLinkedAppdataStore.storeWithOptions([
            KCSStoreKeyResource : collection,
            KCSStoreKeyCachePolicy : KCSCachePolicy.Both.rawValue,
            KCSStoreKeyOfflineUpdateEnabled : true
            ])

Step 4: Upload the Object as follows:

        let imageSendingData = Image()
        imageSendingData.image = UIImage(data: UIImageJPEGRepresentation(image, 0.5)!)
       
        imageToSend.saveObject(imageSendingData, withCompletionBlock: { (objectsOrNil, errorOrNil) -> Void in
            if errorOrNil != nil {
                print("Error saving image: \(errorOrNil)")
            } else {
                print("Save image successful")
            }
        }, withProgressBlock: nil)



In the Console, you should see a new entry created in that collection (with image column storing the _KinveyRef and the newly uploaded file should appear under “Console->Data->Files (Select Files instead of a collection)”).



Thanks,

Pranav

Kinvey Support


Answer

Wood,


Just trying to summarize the steps for uploading images:



Step 1: Define the class :

    class Image : NSObject {    //all NSObjects in Kinvey implicitly implement KCSPersistable
        var entityId: String! //Kinvey entity _id
        var image: UIImage!
    }

Step 2: Override the hostToKinveyPropertyMapping & kinveyPropertyToCollectionMapping method:
   
        override func hostToKinveyPropertyMapping() -> [NSObject : AnyObject]! {
            return [
                "entityId" : KCSEntityKeyId, //the required _id field
                "image" : "image"
            ]
        }
       
        override class func kinveyPropertyToCollectionMapping() -> [NSObject : AnyObject]! {
           
            return [
                "image" : KCSFileStoreCollectionName
            ]
        }
   
Step 3: Initialize store object with KCSLinkedAppDataStore object.

        var collection = KCSCollection(fromString: "Images", ofClass:Image.self)
       
        var imageToSend = KCSLinkedAppdataStore.storeWithOptions([
            KCSStoreKeyResource : collection,
            KCSStoreKeyCachePolicy : KCSCachePolicy.Both.rawValue,
            KCSStoreKeyOfflineUpdateEnabled : true
            ])

Step 4: Upload the Object as follows:

        let imageSendingData = Image()
        imageSendingData.image = UIImage(data: UIImageJPEGRepresentation(image, 0.5)!)
       
        imageToSend.saveObject(imageSendingData, withCompletionBlock: { (objectsOrNil, errorOrNil) -> Void in
            if errorOrNil != nil {
                print("Error saving image: \(errorOrNil)")
            } else {
                print("Save image successful")
            }
        }, withProgressBlock: nil)



In the Console, you should see a new entry created in that collection (with image column storing the _KinveyRef and the newly uploaded file should appear under “Console->Data->Files (Select Files instead of a collection)”).



Thanks,

Pranav

Kinvey Support

Yes I am using KCSPersistable


@interface Examples : NSObject <KCSPersistable>


@property (nonatomic, copy, readonly) NSString *exampleObjectId;

@property (nonatomic, copy) UIImage *exampleImage;


Could you check it on your side , maybe its a defect?


PS: Thank you I will also try to use KCSFileStore

Wood,


Are you using KCSPersistable protocol which is required while mapping classes to Kinvey backend?


Also, you can use KCSFileStore to store and retrieve files using id. Files can be of any format. For more information, please check http://devcenter.kinvey.com/ios/guides/files#Downloading.


Thanks,

Pranav

Login or Signup to post a comment