在UIGraphicsBeginImageContextWithOptions

时间:2018-07-01 12:00:42

标签: ios swift uiimage

我对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!
}

错误原因可能是什么?

MagicalRecord.saveWithBlock中为特定的NSManagedObjectContext调用此方法:

    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

0 个答案:

没有答案