如何使用swift将UIImage保存为字节? [英] How to save UIImage into byte using swift ?

查看:409
本文介绍了如何使用swift将UIImage保存为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想使用iOS swift在sqlite数据库中保存UIImage。我该怎么做?



当我检索字节时,我想将其转换为UIImage。

Hi All,
I want to save UIImage in sqlite database using iOS swift. How can i do this?

Also when i retrieve bytes then i want to convert this into UIImage.

推荐答案

将UIImage转换为字节的技巧: -



1st将UIImage()转换为NSData()。



然后将NSData传递给
Tricks to convert UIImage into byte :-

1st convert UIImage() to NSData() .

Then pass the NSData to the
getArrayOfBytesFromImage() and get the return as NSMutableArray 







func getArrayOfBytesFromImage(imageData:NSData) -> NSMutableArray
{

    // the number of elements:
    let count = imageData.length / sizeof(UInt8)

    // create array of appropriate length:
    var bytes = [UInt8](count: count, repeatedValue: 0)

    // copy bytes into array
    imageData.getBytes(&bytes, length:count * sizeof(UInt8))

    var byteArray:NSMutableArray = NSMutableArray()

    for (var i = 0; i < count; i++) {
        byteArray.addObject(NSNumber(unsignedChar: bytes[i]))
    }

    return byteArray


}







总是建议..我们应该在本地数据库中保存UIImage的NSData




It is always recommended that .. we should save NSData of UIImage in local DB


这篇关于如何使用swift将UIImage保存为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆