带有UIImage的NSOperationQueue导致内存警告

时间:2016-01-19 14:42:04

标签: ios swift nsoperation nsoperationqueue

我正在使用NSOperationQueue将照片上传到远程网络服务器,但是当上传超过10张照片时,会出现内存警告并且RAM使用超过150 Mo.最糟糕的是,一旦照片选择器消失,RAM就不会被取消分配......这是使用的代码:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { () -> Void in
            for i in 0..<self.assets.count {
                let operation: SWSingleUploadOperation = SWUploadManager.sharedManager.operationForPhoto(self.assets[i].editedAsset!,
                    progress: { (progress: Float) -> Void in
                        self.updateCellForOperationIndex(i,
                            withState: .Uploading,
                            andProgress: progress)
                    },
                    completion: { (response: NSHTTPURLResponse?, error: NSError?) -> Void in
                        if response?.statusCode == 200 && error == nil {
                            self.updateCellForOperationIndex(i,
                                withState: .Success,
                                andProgress: 1.0)
                        }
                        else {
                            self.updateCellForOperationIndex(i,
                                withState: .Fail,
                                andProgress: 0.0)
                        }
                })
                operation.delegate = self
                self.cellStates[NSIndexPath(forRow: i, inSection: 0)] = (state: .Uploading, progress: nil)
                self.uploadOperations.append(operation)
                self.uploadQueue?.addOperation(operation)
            }

以下是operationForPhoto使用的方法:

let photoData: NSData = UIImageJPEGRepresentation(photo, 0.8)!
return SWSingleUploadOperation(requestParameters: (uploadID: self.uploadParameters!.uploadID, data: photoData),
                progressHandler: pProgress,
                completionHandler: pCompletion)

这是我的NSOperation子类内容:

var delegate: SingleUploadOperationDelegate?

var requestParameters: (uploadID: String, data: NSData)?
var progressHandler: ((progress: Float) -> Void)?
var completionHandler: ((response: NSHTTPURLResponse?, error: NSError?) -> Void)?

weak var request: Alamofire.Request?

init(requestParameters: (sweebiId: String, data: NSData),
    progressHandler: ((progress: Float) -> Void)?,
    completionHandler: ((response: NSHTTPURLResponse?, error: NSError?) -> Void)?) {
        self.requestParameters = requestParameters
        self.progressHandler = progressHandler
        self.completionHandler = completionHandler
        super.init()
}

override func main() {
    self.delegate?.uploadOperationDidStart(self)

    self.request = Alamofire.upload(Router.AddImageToSweebi(sweebiId: self.requestParameters!.uploadID),
        data: self.requestParameters!.data)
    .validate()
    .progress { (bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) -> Void in
        self.progressHandler?(progress: Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
    }
    .response(completionHandler: { (request: NSURLRequest?, response: NSHTTPURLResponse?, data: NSData?, error: NSError?) -> Void in
        self.requestParameters = nil
        self.progressHandler = nil
        self.completionHandler?(response: response, error: error)
        self.completeOperation()
    })
}

override func cancel() {
    self.request?.cancel()
    self.delegate?.uploadOperationDidCancel(self)
    super.cancel()
}

你知道为什么使用这样的RAM,以及如何避免这种情况并在取消照片选择器后解除分配吗?

0 个答案:

没有答案