簡體   English   中英

ftp圖像上傳導致圖像被迅速切斷

[英]ftp image upload causes image to be cut off swift

我正在測試我在 github 上找到的一些代碼,以通過 ftp 將圖像上傳到我的 IOS 應用程序中的網站。 問題是,當我嘗試上傳半大圖像時,上傳的圖像似乎已損壞,僅顯示頂部,其余部分被切斷。 它似乎適用於較小的圖像或具有較高壓縮率的大圖像。 有誰知道是什么原因造成的?

    let ftpup = FTPUpload(baseUrl: "mysite.com", userName: "user@mysite.com", password: "password", directoryPath: "")

    let image = UIImage(named: "medium")
    let imagedata = UIImageJPEGRepresentation(image!, 1)

    ftpup.send(data: imagedata!, with: "cloudpowa.jpg", success: {(success) -> Void in
        if !success {
            print("Failed upload!")
        }
        else {
            print("image uploaded!")
        }
    })

ftp 代碼來自: https : //gist.github.com/Nirma/fb9991be776107d17fdcd6ed2aa02876

import Foundation
import CFNetwork

public class FTPUpload {
    fileprivate let ftpBaseUrl: String
    fileprivate let directoryPath: String
    fileprivate let username: String
    fileprivate let password: String

    public init(baseUrl: String, userName: String, password: String, directoryPath: String) {
        self.ftpBaseUrl = baseUrl
        self.username = userName
        self.password = password
        self.directoryPath = directoryPath
    }
}


// MARK: - Steam Setup
extension FTPUpload {
    private func setFtpUserName(for ftpWriteStream: CFWriteStream, userName: CFString) {
        let propertyKey = CFStreamPropertyKey(rawValue: kCFStreamPropertyFTPUserName)
        CFWriteStreamSetProperty(ftpWriteStream, propertyKey, userName)
    }

    private func setFtpPassword(for ftpWriteStream: CFWriteStream, password: CFString) {
        let propertyKey = CFStreamPropertyKey(rawValue: kCFStreamPropertyFTPPassword)
        CFWriteStreamSetProperty(ftpWriteStream, propertyKey, password)
    }

    fileprivate func ftpWriteStream(forFileName fileName: String) -> CFWriteStream? {
        let fullyQualifiedPath = "ftp://\(ftpBaseUrl)/\(directoryPath)/\(fileName)"

        guard let ftpUrl = CFURLCreateWithString(kCFAllocatorDefault, fullyQualifiedPath as CFString, nil) else { return nil }
        let ftpStream = CFWriteStreamCreateWithFTPURL(kCFAllocatorDefault, ftpUrl)
        let ftpWriteStream = ftpStream.takeRetainedValue()
        setFtpUserName(for: ftpWriteStream, userName: username as CFString)
        setFtpPassword(for: ftpWriteStream, password: password as CFString)
        return ftpWriteStream
    }
}


// MARK: - FTP Write
extension FTPUpload {
    public func send(data: Data, with fileName: String, success: @escaping ((Bool)->Void)) {

        guard let ftpWriteStream = ftpWriteStream(forFileName: fileName) else {
            success(false)
            return
        }

        if CFWriteStreamOpen(ftpWriteStream) == false {
            print("Could not open stream")
            success(false)
            return
        }

        let fileSize = data.count
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: fileSize)
        data.copyBytes(to: buffer, count: fileSize)

        defer {
            CFWriteStreamClose(ftpWriteStream)
            buffer.deallocate(capacity: fileSize)
        }

        var offset: Int = 0
        var dataToSendSize: Int = fileSize

        repeat {
            let bytesWritten = CFWriteStreamWrite(ftpWriteStream, &buffer[offset], dataToSendSize)
            if bytesWritten > 0 {
                offset += bytesWritten.littleEndian
                dataToSendSize -= bytesWritten
                continue
            } else if bytesWritten < 0 {
                // ERROR
                print("FTPUpload - ERROR")
                break
            } else if bytesWritten == 0 {
                // SUCCESS
                print("FTPUpload - Completed!!")
                break
            }
        } while CFWriteStreamCanAcceptBytes(ftpWriteStream)

        success(true)
    }
}

將 ftp 上傳類中的以下內容從

while CFWriteStreamCanAcceptBytes(ftpWriteStream)

while (true)

我發現它只用前者寫一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM