1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| func resetSizeOfImageData(sourceImage: UIImage!, maxSize: Int) -> NSData { var finallImageData = UIImageJPEGRepresentation(sourceImage,1.0) let sizeOrigin = finallImageData?.count let sizeOriginKB = sizeOrigin! / 1024 if sizeOriginKB <= maxSize { return finallImageData! as NSData } let sourceImageAspectRatio = sourceImage.size.width/sourceImage.size.height var defaultSize = CGSize(width: 1024, height: 1024/sourceImageAspectRatio) let newImage = self.newSizeImage(size: defaultSize, sourceImage: sourceImage) finallImageData = UIImageJPEGRepresentation(newImage,1.0); let compressionQualityArr = NSMutableArray() let avg = CGFloat(1.0/250) var value = avg var i = 250 repeat { i -= 1 value = CGFloat(i)*avg compressionQualityArr.add(value) } while i >= 1
finallImageData = self.halfFuntion(arr: compressionQualityArr.copy() as! [CGFloat], image: newImage, sourceData: finallImageData!, maxSize: maxSize) while finallImageData?.count == 0 { let reduceWidth = 100.0 let reduceHeight = 100.0/sourceImageAspectRatio if (defaultSize.width-CGFloat(reduceWidth)) <= 0 || (defaultSize.height-CGFloat(reduceHeight)) <= 0 { break } defaultSize = CGSize(width: (defaultSize.width-CGFloat(reduceWidth)), height: (defaultSize.height-CGFloat(reduceHeight))) let image = self.newSizeImage(size: defaultSize, sourceImage: UIImage.init(data: UIImageJPEGRepresentation(newImage, compressionQualityArr.lastObject as! CGFloat)!)!) finallImageData = self.halfFuntion(arr: compressionQualityArr.copy() as! [CGFloat], image: image, sourceData: UIImageJPEGRepresentation(image,1.0)!, maxSize: maxSize) } return finallImageData! as NSData } func newSizeImage(size: CGSize, sourceImage: UIImage) -> UIImage { var newSize = CGSize(width: sourceImage.size.width, height: sourceImage.size.height) let tempHeight = newSize.height / size.height let tempWidth = newSize.width / size.width if tempWidth > 1.0 && tempWidth > tempHeight { newSize = CGSize(width: sourceImage.size.width / tempWidth, height: sourceImage.size.height / tempWidth) } else if tempHeight > 1.0 && tempWidth < tempHeight { newSize = CGSize(width: sourceImage.size.width / tempHeight, height: sourceImage.size.height / tempHeight) } UIGraphicsBeginImageContext(newSize) sourceImage.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } func halfFuntion(arr: [CGFloat], image: UIImage, sourceData finallImageData: Data, maxSize: Int) -> Data? { var tempFinallImageData = finallImageData var tempData = Data.init() var start = 0 var end = arr.count - 1 var index = 0 var difference = Int.max while start <= end { index = start + (end - start)/2 tempFinallImageData = UIImageJPEGRepresentation(image, arr[index])! let sizeOrigin = tempFinallImageData.count let sizeOriginKB = sizeOrigin / 1024 print("当前降到的质量:\(sizeOriginKB)\n\(index)----\(arr[index])") if sizeOriginKB > maxSize { start = index + 1 } else if sizeOriginKB < maxSize { if maxSize-sizeOriginKB < difference { difference = maxSize-sizeOriginKB tempData = tempFinallImageData } if index<=0 { break } end = index - 1 } else { break } } return tempData }
|