我正在使用Alamofire,AlamofireImage使用ImageShack存储与Firebase后端缓存图像。有时内存超过 100MB。我想在上传之前学习如何改进我的图像缓存和缩放。例如,我使用该AutoPurgingImageCache在类整个项目中声明了一个公共缓存:
let photoCache = AutoPurgingImageCache(
memoryCapacity: 100 * 1024 * 1024,
preferredMemoryUsageAfterPurge: 60 * 1024 * 1024
)
有什么办法可以定义这些memoryCapacity , preferredMemoryUsageAfterPurge来节省内存?它们到底是什么意思?
我在Alamofire.upload下面使用didFinishPickingImage:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
let urlStr = "http://post.imageshack.us/upload_api.php"
let url = NSURL(string: urlStr)!
let imgData = UIImageJPEGRepresentation(image, 0.25)!
let keyData = "____IMGSHACK_API_KEY_____".dataUsingEncoding(NSUTF8StringEncoding)!
let keyJSON = "json".dataUsingEncoding(NSUTF8StringEncoding)!
let publicData = "no".dataUsingEncoding(NSUTF8StringEncoding)!
Alamofire.upload(.POST, url, headers: nil,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imgData, name:"fileupload", fileName:"image", mimeType: "image/jpg")
multipartFormData.appendBodyPart(data: keyData, name: "key")
multipartFormData.appendBodyPart(data: keyJSON, name: "format")
multipartFormData.appendBodyPart(data: publicData, name: "public")
}, encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.validate()
upload.responseJSON { response in
let responseJSON = response.result.value as? [String: AnyObject]
if let links = responseJSON!["links"] as? Dictionary<String, AnyObject> {
if let imgLink = links["image_link"] as? String {
self.postToFirebase(imgLink)
print(imgLink)
}
}
}
case .Failure(let encodingError):
print(encodingError)
}
})
self.dismissViewControllerAnimated(true, completion: nil)
self.tableView.contentOffset = CGPointMake(0, -60);
}
我认为UIImageJPEGRepresentation(image, 0.25)!不是很好的压缩情况,我的图片来自互联网作为全尺寸。如果我在压缩之前进行缩放以上传,例如:
let size = CGSize(width: 500.0, height: 273.0)
let scaledImage = image.af_imageScaledToSize(size)
我认为网络平台的图像质量可能很差。所以我在图像下载后进行缩放和缓存:
//MARK: - Image Downloading
func getNetworkImage(urlString: String, comesFrom: String, completion: (UIImage -> Void)) -> (Request) {
return Alamofire.request(.GET, urlString).responseImage { (response) -> Void in
guard let image = response.result.value else { return }
completion(image)
if comesFrom == "collectionPage" {
self.cacheCollectionImage(image, urlString: urlString)
}
else if comesFrom == "postPage" {
self.cachePostImage(image, urlString: urlString)
}
}
}
//MARK: - Image Caching
func cacheCollectionImage(image: Image, urlString: String) {
let size = CGSize(width: 188.0, height: 120.0)
let scaledImage = image.af_imageScaledToSize(size)
photoCache.addImage(scaledImage, withIdentifier: urlString)
}
func cachePostImage(image: Image, urlString: String) {
let size = CGSize(width: 300.0, height: 165.0)
let scaledImage = image.af_imageScaledToSize(size)
photoCache.addImage(scaledImage, withIdentifier: urlString)
}
func cachedImage(urlString: String) -> Image? {
print("cachedImage() func ended")
return photoCache.imageWithIdentifier(urlString)
}
我的应用程序也以 40MB 的内存使用打开。在上传和滚动以返回AutoPurgingImageCache工作并减少内存使用后,我看到最大最高使用量为 130MB ,但我认为仍然如此。
你有什么建议我在这些情况下节省内存?我需要一些解释,你想说什么工具或策略来尽可能地为每个平台处理它的质量。