Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 10-Apr-17 22:53pm

1 solution

Tricks to convert UIImage into byte :-

1st convert UIImage() to NSData() .

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


Objective-C
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


}



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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900