Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload images to Azure using Swift

I am developing an app which needs to store images in Azure using Swift.

Is there any example that will be helpful ? If not can you give me a suggestion ?

like image 717
Subin K Kuriakose Avatar asked Jun 02 '15 04:06

Subin K Kuriakose


1 Answers

Here is a simple example.

1- Start here: https://azure.microsoft.com/en-us/documentation/articles/storage-ios-how-to-use-blob-storage/

2- Get the SDK

3- Here is the code:

let account = AZSCloudStorageAccount(fromConnectionString:AZURE_STORAGE_CONNECTION_STRING) //I stored the property in my header file
let blobClient: AZSCloudBlobClient = account.getBlobClient()
let blobContainer: AZSCloudBlobContainer = blobClient.containerReferenceFromName("<yourContainerName>")
blobContainer.createContainerIfNotExistsWithAccessType(AZSContainerPublicAccessType.Container, requestOptions: nil, operationContext: nil) { (NSError, Bool) -> Void in
   if ((NSError) != nil){
      NSLog("Error in creating container.")
   }
   else {
      let blob: AZSCloudBlockBlob = blobContainer.blockBlobReferenceFromName(<nameOfYourImage> as String) //If you want a random name, I used let imageName = CFUUIDCreateString(nil, CFUUIDCreate(nil))          
      let imageData = UIImagePNGRepresentation(<yourImageData>)

      blob.uploadFromData(imageData!, completionHandler: {(NSError) -> Void in
      NSLog("Ok, uploaded !")
      })
    }
}

Enjoy :)

like image 131
el_even Avatar answered Nov 03 '22 02:11

el_even