我有 UIImage 的简单扩展:

func scaledWithMaxWidthOrHeightValue(value: CGFloat) -> UIImage? {

    let width = self.size.width
    let height = self.size.height

    if width <= value && height <= value {
        return self
    }

    let ratio = width/height

    var newWidth = value
    var newHeight = value

    if ratio > 1 {
        newWidth = min(width, value)
        newHeight = height * (newWidth/width)
    } else {
        newHeight = min(height, value)
        newWidth = width * (newHeight/height)
    }

    UIGraphicsBeginImageContextWithOptions(CGSize(width: newWidth, height: newHeight), false, 0)

    draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) //Thread 11: EXC_BAD_ACCESS (code=1, address=0x28)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image!
}

可能是错误的原因是什么?

对于特定的 NSManagedObjectContext ,在 MagicalRecord.saveWithBlock 内调用此方法:

if let asset = record["image"] as? CKAsset {
        do {
            card!.imageData = try Data(contentsOf: asset.fileURL)
            if let imageData = card!.imageData, let image = UIImage(data: imageData) {
                if let thumbnailImage = image.scaledWithMaxWidthOrHeightValue(value: 200) {
                    card?.thumbnailImageData = UIImagePNGRepresentation(thumbnailImage)
                }
            }
        } catch {}
    }

这是在控制台上输出:

Territories(3674,0x16b90f000) malloc: *** mach_vm_map(size=983040) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Territories(3674,0x16b90f000) malloc: *** mach_vm_map(size=425984) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
2018-07-01 21:34:07.114622+0200 Territories[3674:1366995] [Unknown process name] CGSImageDataLock: Cannot allocate memory