using cloudkit to retrieve and update images

I have an app that requires me to update images on a weekly basis. I have found that the way to go for this is cloudKit, but I am having trouble updating retreiving and creating images that i can put into image views on my app. Basically I need help on putting updatable cloud kit images into UIImage views inside my app - I need to be able to regularly update the content of the images using cloudKit.


Any help is appreciated

Accepted Reply

It is unclear what you are asking. But the answer may be to place the UIImage as an object in a dictionary and then create an NSData object from that dictionary and store the NSData object. Then retrieve the NSData object and recreate the UIImage. These two commands will help:


    NSData *imageData = [NSPropertyListSerialization dataWithPropertyList:dictionaryWithImageObject
                        format:NSPropertyListXMLFormat_v1_0 options:0 error:&error1 ];

//and


    NSDictionary *dictionaryWithImageObject =
              (NSDictionary *)[NSPropertyListSerialization    
                            propertyListWithData:imageData
                            options:NSPropertyListMutableContainersAndLeaves
                            format:&format1
                            error:&errorDesc1];

Replies

It is unclear what you are asking. But the answer may be to place the UIImage as an object in a dictionary and then create an NSData object from that dictionary and store the NSData object. Then retrieve the NSData object and recreate the UIImage. These two commands will help:


    NSData *imageData = [NSPropertyListSerialization dataWithPropertyList:dictionaryWithImageObject
                        format:NSPropertyListXMLFormat_v1_0 options:0 error:&error1 ];

//and


    NSDictionary *dictionaryWithImageObject =
              (NSDictionary *)[NSPropertyListSerialization    
                            propertyListWithData:imageData
                            options:NSPropertyListMutableContainersAndLeaves
                            format:&format1
                            error:&errorDesc1];

To clarify, I want to have an image view that I can update the image inside of it, without releasing a new version of my app... I heard that I needed to use CloudKit to do this, and I was wondering how I would go about doing this, if CloudKit is not the way to do it... please give me another solution... if it is, please explain to me how to god it using CloudKit.

You could place the image, as an NSData object, in CloudKit and the app would download it. You could also place the image on a website and the app could download it from that website. See: https://forums.developer.apple.com/thread/101675

Post not yet marked as solved Up vote reply of PBK Down vote reply of PBK

Great! That was very helpful. Could you possibly explain, or put a link to a video or documentation that explains how to place the image as an NSData object in cloudKit?

Also, do I do this through the cloudKit dashboard, or xCode? If it is both,, I would prefer an explanation on the Cloudkit dashboard way.

Again, Any help is appreciated!

But, if it is easier for you, you could explain the Xcode way - like through commands etc.

I am sorry but you will need to learn how to program CloudKit following the various Apple documents on CloudKit or various help books and tutorials; I really can't walk you through the code.


I'd do it all from the app. You can run a modified version of the production app from XCode and have that modified version run from XCode still point to the production environment in CloudKit. Using the modified app running from XCode you could grab any image that you add to XCode and post it to CloudKit. The two lines of code above let you post and retrieve an image from CloudKit.


Good luck.

Thank You! could you link the apple developer documentation?

There's a direct way to do this. In the dashboard, you can create a field as Bytes. When creating a new record, you can browse for your image file (via a Choose File button) or drag it into the field. I use .png files.


Then in your code, in the completion handler where you fetch records from CloudKit, use


if let pngData = record?.object(forKey: "your image field") as? Data {

let myImage = UIImage(data: pngData, scale: 1)

}


where record is the requested (CKRecord?) record object.


This separates the daily/weekly/monthly changing of images from your code to read those images, doesn't require a one-off version of code, and doesn't require web page.