的UIImage切片造成死机
问题描述:
我使用下面的代码来削减图像的瓷砖,并把每一个集合中:的UIImage切片造成死机
import UIKit
extension UIImage {
func image(withRotation radians: CGFloat) -> UIImage {
let cgImage = self.cgImage!
let LARGEST_SIZE = CGFloat(max(self.size.width, self.size.height))
let context = CGContext.init(data: nil, width:Int(LARGEST_SIZE), height:Int(LARGEST_SIZE), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: cgImage.colorSpace!, bitmapInfo: cgImage.bitmapInfo.rawValue)!
var drawRect = CGRect.zero
drawRect.size = self.size
let drawOrigin = CGPoint(x: (LARGEST_SIZE - self.size.width) * 0.5,y: (LARGEST_SIZE - self.size.height) * 0.5)
drawRect.origin = drawOrigin
var tf = CGAffineTransform.identity
tf = tf.translatedBy(x: LARGEST_SIZE * 0.5, y: LARGEST_SIZE * 0.5)
tf = tf.rotated(by: CGFloat(radians))
tf = tf.translatedBy(x: LARGEST_SIZE * -0.5, y: LARGEST_SIZE * -0.5)
context.concatenate(tf)
context.draw(cgImage, in: drawRect)
var rotatedImage = context.makeImage()!
drawRect = drawRect.applying(tf)
rotatedImage = rotatedImage.cropping(to: drawRect)!
let resultImage = UIImage(cgImage: rotatedImage)
return resultImage
}
}
class Image {
static let shared:Image = Image()
var image:UIImage = UIImage(named:"Me")!
var images:[UIImage] = []
func shuffle(model:[Int], imageRef:UIImage) {
image = imageRef
images = self.slice(into: Int(sqrt(Double(model.count))))
}
func slice(into howMany: Int) -> [UIImage] {
let width: CGFloat
let height: CGFloat
switch image.imageOrientation {
case .left, .leftMirrored:
image = image.image(withRotation: (CGFloat)(Double.pi/2 ))
break
case .right, .rightMirrored:
image = image.image(withRotation: (CGFloat)(-Double.pi/2))
break
default:
break
}
width = image.size.width
height = image.size.height
let tileWidth = Int(width/CGFloat(howMany))
let tileHeight = Int(height/CGFloat(howMany))
let scale = Int(image.scale)
var images = [UIImage]()
let cgImage = image.cgImage!
var adjustedHeight = tileHeight
var y = 0
for row in 0 ..< howMany {
if row == (howMany - 1) {
adjustedHeight = Int(height) - y
}
var adjustedWidth = tileWidth
var x = 0
for column in 0 ..< howMany {
if column == (howMany - 1) {
adjustedWidth = Int(width) - x
}
let origin = CGPoint(x: x * scale, y: y * scale)
let size = CGSize(width: adjustedWidth * scale, height: adjustedHeight * scale)
let tileCgImage = cgImage.cropping(to: CGRect(origin: origin, size: size))
images.append(UIImage(cgImage: tileCgImage!, scale: image.scale, orientation: image.imageOrientation))
x += tileWidth
}
y += tileHeight
}
return images
}
}
在情况下的howmany比15大,我只得到坠毁,机上真实的设备,并带有以下消息: “来自调试器的消息:由于内存问题而终止”
任何人都可以协助?
唯一的原因,我能想出的是,每一次创建的映像采取太多的空间,如在15的情况下将有15^2 =图像225
即使与考虑,我还是想不到一种方式我可以做到这一点不同..
任何人有任何想法?
答
尝试将代码循环添加到autoreleasepool。
autoreleasepool {
if column == (howMany - 1) {
adjustedWidth = Int(width) - x
}
let origin = CGPoint(x: x * scale, y: y * scale)
let size = CGSize(width: adjustedWidth * scale, height: adjustedHeight * scale)
let tileCgImage = cgImage.cropping(to: CGRect(origin: origin, size: size))
images.append(UIImage(cgImage: tileCgImage!, scale: image.scale, orientation: image.imageOrientation))
x += tileWidth
}
仍然收到错误。见上面的更新.. – YanivH