nsdata

How to initialise a string from NSData in Swift

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 05:01:29
问题 I have been trying to initialise a string from NSData in Swift. In the NSString Cocoa Documentation Apple is saying you have to use this: init(data data: NSData!, encoding encoding: UInt) However Apple did not include any example for usage or where to put the init . I am trying to convert the following code from Objective-C to Swift NSString *string; string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; I have been trying a lot of possible syntaxes such as the

Byte Array to NSData

主宰稳场 提交于 2019-11-27 04:25:15
In a WebService JSON response is coming. In the response, there is image is coming as a byte array. I have to show the image in a UIImageView. I am trying to convert the byte array to NSData. But not getting how to do that. Any help would be appreciated. I am confident that the byte array has image data in it. Sample Byte array for your reference: (137, 80, 78, 71, ... 66, 96, 130) Thanks You have to convert the JSON to an array of strings first; you can, for example, use the NSJSONSerialization class : NSArray *strings = [NSJSONSerialization JSONObjectWithData:theJSONString options

How to downscale a UIImage in IOS by the Data size

徘徊边缘 提交于 2019-11-27 04:14:40
问题 I am looking to downscale a UIImage in iOS . I have seen other questions below and their approach on how to downscale the image by size. Resizing Images Objective-C How to resize the image programmatically in objective-c in iphone The simplest way to resize an UIImage? These questions are all based on re-sizing the image to a specific size. In my case I am looking to re-size/downscale the image based on a maximum size. As an example, I would like to set a maximum NSData size to be 500 KB. I

NSData-AES Class Encryption/Decryption in Cocoa

家住魔仙堡 提交于 2019-11-27 03:28:46
I am attempting to encrypt/decrypt a plain text file in my text editor. encrypting seems to work fine, but the decrypting does not work, the text comes up encrypted. I am certain i've decrypted the text using the word i encrypted it with - could someone look through the snippet below and help me out? Thanks :) Encrypting: NSAlert *alert = [NSAlert alertWithMessageText:@"Encryption" defaultButton:@"Set" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Please enter a password to encrypt your file with:"]; [alert setIcon:[NSImage imageNamed:@"License.png"]]; NSSecureTextField

iOS UIImageJPEGRepresentation error: Not a JPEG file: starts with 0xff 0xd9

99封情书 提交于 2019-11-27 03:20:57
问题 I am writing a .jpg file to my app's Documents directory like this: NSData *img = UIImageJPEGRepresentation(myUIImage, 1.0); BOOL retValue = [img writeToFile:myFilePath atomically:YES]; Later, I load that image back into a UIImage using: UIImage *myImage = [UIImage imageWithContentsOfFile:path]; I know it works because I can draw the image in a table cell and it is fine. Now if I try to use UIImageJPEGRepresentation(myImage, 1.0), the debugger prints out these lines: <Error>: Not a JPEG file:

Inserting NSData into SQLite on the iPhone

人走茶凉 提交于 2019-11-27 02:55:34
问题 So far I've managed to create this method for inserting into a SQLite database on the iPhone: - (void) insertToDB :(NSString *)Identifier :(NSString *)Name { sqlite3 *database; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { char *sql1 = "INSERT INTO table VALUES ('"; const char *sql2 = [Identifier cStringUsingEncoding:[NSString defaultCStringEncoding]]; char *sql3 = "', '"; const char *sql4 = [Name cStringUsingEncoding:[NSString defaultCStringEncoding]]; char *sql5 = "'

Convert any Data Type into NSData and back again

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 02:48:42
问题 I'm working on an app where you can send either a text, image or a contact over Multipeer Connectivity to another device. It will then save in the second device's Core Data. What I do is send the data over as an NSDictionary and convert it back again. So I am left with an NSDictionary on the receiving device. How then, can I save the object for the key of @"objectData" to Core Data? I'd like it to work with NSString, UIImage & ABPerson. // Create a new object in the managed object context.

iPhone and GZip

雨燕双飞 提交于 2019-11-27 01:42:54
问题 Now I know that xCode automaticly does the GZip decrompession for you within: NSData *data = [NSData dataWithContentsOfURL:URL]; And it does work if I point to a Gzip file on my server. But since my content is dynamic, I have a PHP script that rather then create a gzip file like so: $zp = gzopen($file, "r"); $data = gzread($zp, $filesize); gzclose($zp); I encode my own data with: echo gzencode($data, 9); With this I add the following headers: header("Content-Type: application/x-gzip"); header

Can't convert NSData to NSString in swift

旧城冷巷雨未停 提交于 2019-11-27 01:41:12
问题 This is my function. First println print correct hash to console but in the next row program crashes. Can you help me? func sha256(string: NSString) -> NSString { var data : NSData! = string.dataUsingEncoding(NSUTF8StringEncoding) var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0) CC_SHA256(data.bytes, CC_LONG(data.length), &hash) let res = NSData(bytes: hash, length: Int(CC_SHA256_DIGEST_LENGTH)) println(res) let resstr = NSString(data: res, encoding:

Guess encoding when creating an NSString from NSData

核能气质少年 提交于 2019-11-27 01:21:41
问题 When reading an NSString from a file I can use initWithContentsOfFile:usedEncoding:error: and it will guess the encoding of the file. When I create it from an NSData though my only option is initWithData:encoding: where I have to explicitly pass the encoding. How can I reliably guess the encoding when I work with NSData instead of files? 回答1: In general, you can’t. However, you can quite reliably identify UTF-8 files – if a file is valid UTF-8, it’s not very likely that it’s supposed to be