nsdata

Convert UIImage to NSData and convert back to UIImage in Swift?

ⅰ亾dé卋堺 提交于 2019-11-27 17:16:13
I'm trying to save a UIImage to NSData and then read the NSData back to a new UIImage in Swift. To convert the UIImage to NSData I'm using the following code: let imageData: NSData = UIImagePNGRepresentation(myImage) How do I convert imageData (i.e., NSData ) back to a new UIImage ? algal UIImage(data:imageData,scale:1.0) presuming the image's scale is 1. In swift 4.2, use below code for get Data(). image.pngData() Ivan Sinigaglia Thanks. Helped me a lot. Converted to Swift 3 and worked To save: let data = UIImagePNGRepresentation(image) To load: let image = UIImage(data: data) Use

Storing images to CoreData - Swift

纵饮孤独 提交于 2019-11-27 16:55:39
问题 In my code I managed to save a textLabel with CoreData but I can't seem to properly save the image. I've read some tutorials and I know I have to convert it to NSData. But how do I do it? Thanks in advance! 回答1: Core Data isn't meant to save big binary files like images. Use Document Directory in file system instead. Here is sample code to achieve that. let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String // self.fileName is

Converting NSData to SecKeyRef

ⅰ亾dé卋堺 提交于 2019-11-27 15:25:43
问题 i have a public key which i gathered from a remote server and i want to perform RSA encryption with that public key. But the problem is i get the public key data as byte array in buffer. I can convert it to NSData but i can not convert to SecKeyRef so i can keep going with encryption. My encryption code is like: +(NSString *)encryptRSA:(NSString *)plainTextString withKey:(SecKeyRef)publicKey { size_t cipherBufferSize = SecKeyGetBlockSize(publicKey); uint8_t *cipherBuffer = malloc

Saving [UIColor colorWithPatternImage:image] UIColor to Core Data using NSKeyedArchiver

霸气de小男生 提交于 2019-11-27 15:24:19
I'm unable to create an NSData object from a UIColor (with a pattern) created with the factory method [UIColor colorWithPatternImage:image] works fine for standard UIColor objects. Wondering if there is another way to save a UIColor with a pattern into Core Data. I am using the following code to archive the UIColor (with a pattern)... - (id)transformedValue:(id)value { NSData *data = [NSKeyedArchiver archivedDataWithRootObject:value]; return data; } and these are the errors I'm receiving... -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding

How to convert AVAsset to NSData or save it to file manager

匆匆过客 提交于 2019-11-27 14:59:05
问题 I am trying to do that using AVAsset to record Audio file and then after first i store it on NSFileManager and after that by convert it to nsdata i Call API to store it. I am successful to Create AVAsset recording file and play it using third party Class that is SCPlayer . Now problem is that i don't know how to Use AVAsset file for save it in file manager and then after call API to sent it by converting it to NSData . Is any way to convert AVAsset to NSData ??? Please Help... 回答1: You can do

Convert hex data string to NSData in Objective C (cocoa)

有些话、适合烂在心里 提交于 2019-11-27 14:46:43
fairly new iPhone developer here. Building an app to send RS232 commands to a device expecting them over a TCP/IP socket connection. I've got the comms part down, and can send ASCII commands fine. It's the hex code commands I'm having trouble with. So lets say I have the following hex data to send (in this format): \x1C\x02d\x00\x00\x00\xFF\x7F How do I convert this into an NSData object, which my send method expects? Obviously this does not work for this hex data (but does for standard ascii commands): NSString *commandascii; NSData *commandToSend; commandascii = @"\x1C\x02d\x00\x00\x00\xFF

Correct way to load image into UIWebView from NSData object

╄→гoц情女王★ 提交于 2019-11-27 13:38:07
I have downloaded a gif image into an NSData object (I've checked the contents of the NSData object and it's definitely populated). Now I want to load that image into my UIWebView. I've tried the following: [webView loadData:imageData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil]; but I get a blank UIWebView. Loading the image from the same URL directly works fine: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]]; [imageView loadRequest:request]; Do I need to set the textEncodingName to something, or am I doing something else wrong? I want to load

Retrieve nsdata from sqlite stored in Blob data type for iPhone App

﹥>﹥吖頭↗ 提交于 2019-11-27 13:35:05
问题 In iPhone App how to store UIImage converted into NSData to sqlite table in BLOB datatype? Is there any kind of binding needed( NSData ->Blob)? or While retrieving image stored in NSData form in sqlite Blob datatype, should I need to perform any task to convert that stored database in blob datatype to get back into NSData form? Please help and suggest. 回答1: I used something like this: // prepare sqlite3 statement before this line int res = sqlite3_step(stmt); if (res == SQLITE_ROW) { const

Send and receive NSData via GameKit

℡╲_俬逩灬. 提交于 2019-11-27 13:22:45
I'm trying to send some NSData over Bluetooth through GameKit . While I've got GameKit set up and are able to send small messages across, I now would like to expand and send across whole files. I've been reading that you have to split large files up into packets before sending them across individually. So I decided to create a struct to make it easier to decode the packets when they're received at the other end: typedef struct { const char *fileName; NSData *contents; int fileType; int packetnumber; int totalpackets; } file_packet; However, for small files (8KB and less) I thought one packet

Writing Data to an NSOutputStream in Swift 3

本小妞迷上赌 提交于 2019-11-27 13:14:25
问题 The solution of this question no longer works with Swift 3. There is no longer a property bytes of Data (formerly NSData . let data = dataToWrite.first! self.outputStream.write(&data, maxLength: data.count) With this code, I get the error: Cannot convert value of type 'Data' to expected argument type 'UInt8' How can you write Data to an NSOutputStream in Swift 3? 回答1: NSData had a bytes property to access the bytes. The new Data value type in Swift 3 has a withUnsafeBytes() method instead,