使用POST ios SWIFT

时间:2016-06-08 15:23:21

标签: php ios mysql video base64

我正在尝试将图片和视频发送到我的在线mySQL数据库并将其存储为blob。首先,我压缩它们并将它们存储为base64编码的字符串。为此,我有以下两个功能:

FOR PHOTOS:

 func compressImage(image:UIImage) -> NSData {
    // Reducing file size to a 10th

    var actualHeight : CGFloat = image.size.height
    var actualWidth : CGFloat = image.size.width
    let maxHeight : CGFloat = 1136.0
    let maxWidth : CGFloat = 640.0
    var imgRatio : CGFloat = actualWidth/actualHeight
    let maxRatio : CGFloat = maxWidth/maxHeight
    var compressionQuality : CGFloat = 0.5

    if (actualHeight > maxHeight || actualWidth > maxWidth){
        if(imgRatio < maxRatio){
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = maxHeight;
        }
        else if(imgRatio > maxRatio){
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = maxWidth;
        }
        else{
            actualHeight = maxHeight;
            actualWidth = maxWidth;
            compressionQuality = 1;
        }
    }

FOR VIDEOS:

func compressVideo(inputURL: NSURL, outputURL: NSURL, handler:(session: AVAssetExportSession)-> Void)
{
    let urlAsset = AVURLAsset(URL: inputURL, options: nil)

    let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality)

    exportSession!.outputURL = outputURL

    exportSession!.outputFileType = AVFileTypeQuickTimeMovie

    exportSession!.shouldOptimizeForNetworkUse = true

    exportSession!.exportAsynchronouslyWithCompletionHandler { () -> Void in

        handler(session: exportSession!)
    }

}

然后执行以下行:

FOR PHOTOS:

 let compressedImageNSData = self.compressImage(currImage)
           let base64encodedImage = compressedImageNSData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

FOR VIDOES:

let data = NSData(contentsOfURL: self.uploadUrl)
let base64encodedVideo = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

最后,我使用NSURLSession将编码的字符串作为参数放在POST请求中。我已经使用我的iPad2进行了测试,照片看起来很好,最长5秒的视频似乎也能正常工作,但是比这更长的时间导致我的PHP脚本中的INSERT INTO SQL查询失败。我是正确地做这个还是有办法进一步减少字符串的大小,或者我应该完全使用不同的方法。

更新:

我在phpmyAdmin中发现了以下限制:  enter image description here

有没有办法可以进一步缩小视频的文件大小?

0 个答案:

没有答案