How can I upload images to Parse.com programatically? - ios

I have a very large set of images to upload, and the best way to do that is to let my app do it for me. But after copy and pasting the code from Parse's docs, it doesn't work.
var image = NSData(contentsOfURL: NSURL(string: "XY1_EN_1.jpg")!)!
#IBAction func xyButton1(sender: AnyObject) {
for var i = 1; i < 147; i++ {
let imageData = UIImageJPEGRepresentation(image)
//ERROR on line above: Missing argument for parameter #2 in call
let imageFile = PFFile(name:"image.png", data:imageData)
var userPhoto = PFObject(className:"Cards")
userPhoto["imageName"] = "XY1_EN_1"
userPhoto["imageFile"] = imageFile
userPhoto.save()
}
}
What am I doing wrong?

There are two problems with the code. UIImageJPEGRepresentation(::) takes two parameters, a UIImage and a float. Right now you are calling UIImageJPEGRepresentation(_:) and giving it NSData. If you want to use that method to get NSData from an Image, you need to make self.image of UIImage type instead of NSData. Maybe init your variable image with UIImage(named: "imageName") if the image is bundled.

Related

Displaying image in Swift

There are many examples on how to use imageWithContentsOfFile in object-c, but how do I use it in Swift. imageView.image = UIImage(imageWithContentsOfFile: imageName)
returns Argument labels '(imageWithContentsOfFile:)' do not match any available overloads
while if I write: imageView.image = UIImage(contentsOfFile:imageName)
It doesn't display the image.
(I have all images stores in Media.xcassets)
I am displaying a lot of images so using UIImage(named:imageName) isn't a viable option. How can I get the functionality of contentsOfFile in swift?
Swift code:
func getImage (named name : String) -> UIImage?
{
if let imgPath = Bundle.main.path(forResource: name, ofType: ".png")
{
return UIImage(contentsOfFile: imgPath)
}
return nil
}
//Image name "MyImage.png"
getImage(named: "MyImage")!

I want to encode and decode a UIImage to and from a String for a value in AEXML in swift

//To encode I found the following example. I don't know if it works yet
#IBAction func exitImageViewController(seque:UIStoryboardSegue){
let imageController = seque.destination as! MyImageViewController
let myView = imageController.imageView
imageData = UIImagePNGRepresentation((myView?.image!)!)
imageString = imageData?.base64EncodedString()
// Is this swift code correct?
//I cannot find an example of decoding String back to UIImage
The code you posted looks fine. To encode back you can do this:
let data = Data(base64Encoded: imageString!)
let img = UIImage(data: data!)

Why can't I upload images to Parse? Anybody know of a different way to do this?

I just need to upload some images, and I feel like my simple code should work, but for some reason it isn't. I'm getting an error saying that my object exceeds the Parse.com limit of 128kb... And I'm sure it doesn't actually exceed that. Code is here.
override func viewDidLoad() {
super.viewDidLoad()
func addCards(urlString:String) {
var newCard = PFObject(className: "Cards")
let url = NSURL(string: urlString)
let urlRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
newCard["image"] = data
newCard.save()
})
}
addCards("http://image.com/image")
You shouldn't just be pushing the image data direct into the object. Instead, create a PFFile instance with the data and set that. Then, save the file and the card at the same time (using saveAll).
See the following link from Parse documentations which has a code snippet and also the reason for using PFFile as suggested by Wain:
https://www.parse.com/docs/ios/guide#files
According to Parse documentation: You can easily store images by converting them to NSData and then using PFFile. Suppose you have a UIImage named image that you want to save as a PFFile:
let imageData = UIImagePNGRepresentation(image)
let imageFile = PFFile(name:"image.png", data:imageData)
var userPhoto = PFObject(className:"UserPhoto")
userPhoto["imageName"] = "My trip to Hawaii!"
userPhoto["imageFile"] = imageFile
userPhoto.saveInBackground()

Retrieving saved image using swift

I am trying to retrieve an image saved using:
let pngData = UIImagePNGRepresentation(image)
let finalPath = filePath() // This method just create the path to save to.
saveImage(pngData, filePath: finalPath)
Later I want to retrieve this data and set the UIImageView of a UIButton. However when I use the following code, it just displays a completely blue image.
Here is the code:
let filePath = tempCard?.frontPhoto
let imgData = UIImage(contentsOfFile: filePath!)frontPhotoButton.setImage(imgData, forState: .Normal)
frontPhotoButton.setImage(imgData, forState: .Normal)
I am not sure why this is just showing a blue button.
Edit:
I have also tried:
let filePath = tempCard?.frontPhoto
let imageData = NSData(contentsOfFile: filePath!
let image = UIImage(data: imageData!)
frontPhotoButton.setImage(image, forState: .Normal)
Same result.
For anyone else who has a problem in the future setting the background image of a UIButton. Both of the above code snippets work to retrieve the image.
The Problem was, in interface builder my button was: Type - System. When I changed Type - Custom everything worked fine.
Use this method to retrieve image from document directory of an app
func loadImageFromPath() {
dispatch_async(dispatch_get_main_queue(), {
let fileManager = NSFileManager.defaultManager()
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var getImagePath = paths.stringByAppendingPathComponent("Image2.png")
if (fileManager.fileExistsAtPath(getImagePath))
{
println("FILE AVAILABLE AT \(paths)");
//Pick Image and Use accordingly
var imageis: UIImage = UIImage(contentsOfFile: getImagePath)!
let data: NSData = UIImagePNGRepresentation(imageis)
self.imgProfileView.image = UIImage(data: data)
self.imgProfileView.contentMode = UIViewContentMode.ScaleAspectFit
}
else
{
println("FILE NOT AVAILABLE");
}
});
}

Display image from URL in swift, unwrapping error

I'm trying to display image to imageView from URL. I successfully done it using synchronous method in a simple project. But this application is used for online store, so I took product information and image URL from a JSON file. which I'm sure I was successfully stored all the data in a product array. But the problem I'm facing is this:
fatal error: unexpectedly found nil while unwrapping an Optional value
Here is the code.
// UnWrapping imageURL
let url:NSURL? = NSURL(string: actualCurrentProduct.imageURL)
if let actualURL = url {
let imageData = NSData(contentsOfURL: actualURL)
if let actualImageData = imageData {
self.productImageView.image = UIImage(data: actualImageData)
}
}
It highlighted in this particular code.
self.productImageView.image = UIImage(data: actualImageData)
Anybody have any idea why? Really appreciated it.
I managed to display the image successfully from URL with this code. I started the project from the scratch and for the display image part, here is my code.
let imageURL = NSURL(string: actualCurrentProduct.imageURL)
if let actualImageURL = imageURL {
let imageData = NSData(contentsOfURL: actualImageURL)
if let actualImageData = imageData {
let image = UIImage(data: actualImageData)
if let actualImage = image {
self.productImage.image = actualImage
}
}
}
Finally....

Resources