使用UIImagePickerController与Alamofire合作上传图像

时间:2016-03-24 03:59:28

标签: swift uiimagepickercontroller alamofire

我在我的应用程序中使用UITabBarController。让我们说调度表单有步骤,所以每个TabItem都是步骤。在步骤3(表3)有一个附件,使用UIImagePickerController这样的照片上传。

import UIKit

class AttachmentViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

@IBOutlet weak var photoCollectionView: UICollectionView!

var photo = [Photo]()

//..... Collection View Delegate and Datasource for displaying images

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    var newImage: UIImage

    if let possibleImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        newImage = possibleImage
    } else if let possibleImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
        newImage = possibleImage
    } else {
        return
    }

    let imageName = NSUUID().UUIDString
    let imagePath = getDocumentsDirectory().stringByAppendingPathComponent(imageName)

    if let jpegData = UIImageJPEGRepresentation(newImage, 80) {
        jpegData.writeToFile(imagePath, atomically: true)
    }

    let finalPhoto = Photo(image: imageName)
    photo.append(finalPhoto)
    photoCollectionView.reloadData()

    dismissViewControllerAnimated(true, completion: nil)
}

问题是我不知道如何在swift中使用Alamofire上传带有字节的图像。我现在可以获取图像,我只是不知道如何使用alamofire上传它。

1 个答案:

答案 0 :(得分:0)

转换为base64字符串并上传

 let image : UIImage = UIImage("your image")
            let imageData = UIImageJPEGRepresentation(image, 0.1)
            base64String = imageData!.base64EncodedStringWithOptions([])