我使用以下代码将图片剪切到图块并将每个图片放在一个集合中:
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,我只会在真实设备上崩溃,并显示以下消息: &#34; 来自调试器的消息:由于内存问题而终止&#34;
有人可以帮忙吗?
我能想到的唯一原因是每次创建的图像占用的空间都很大,因为在15的情况下会有15 ^ 2个图像= 225。
即使考虑到这一点,我仍然无法想到我可以这样做的方式......
任何人都有任何想法?
答案 0 :(得分:0)
尝试将循环中的代码添加到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
}