-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserMedia.swift
executable file
·59 lines (47 loc) · 1.72 KB
/
UserMedia.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// UserMedia.swift
//
//
// Created by Andrei Gurau on 2/21/16.
//
//
import UIKit
import Parse
class UserMedia: NSObject {
/**
* Other methods
*/
/**
Method to post user media to Parse by uploading image file
- parameter image: Image that the user wants upload to parse
- parameter caption: Caption text input by the user
- parameter completion: Block to be executed after save operation is complete
*/
class func postUserImage(image: UIImage?, withCaption caption: String?, withCompletion completion: PFBooleanResultBlock?) {
// Create Parse object PFObject
let media = PFObject(className: "UserMedia")
// Add relevant fields to the object
media["media"] = getPFFileFromImage(image) // PFFile column type
media["author"] = PFUser.currentUser() // Pointer column type that points to PFUser
media["caption"] = caption
media["likesCount"] = 0
media["commentsCount"] = 0
// Save object (following function will save the object in Parse asynchronously)
media.saveInBackgroundWithBlock(completion)
}
/**
Method to post user media to Parse by uploading image file
- parameter image: Image that the user wants upload to parse
- returns: PFFile for the the data in the image
*/
class func getPFFileFromImage(image: UIImage?) -> PFFile? {
// check if image is not nil
if let image = image {
// get image data and check if that is not nil
if let imageData = UIImagePNGRepresentation(image) {
return PFFile(name: "image.png", data: imageData)
}
}
return nil
}
}