NSDictionary to NSData and NSData to NSDictionary in Swift

You can use NSKeyedArchiver and NSKeyedUnarchiver Example for swift 2.0+ var dictionaryExample : [String:AnyObject] = [“user”:”UserName”, “pass”:”password”, “token”:”0123456789″, “image”:0] let dataExample : NSData = NSKeyedArchiver.archivedDataWithRootObject(dictionaryExample) let dictionary:NSDictionary? = NSKeyedUnarchiver.unarchiveObjectWithData(dataExample)! as? NSDictionary Swift3.0 let dataExample: Data = NSKeyedArchiver.archivedData(withRootObject: dictionaryExample) let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: dataExample) as! [String : Any] Screenshot of playground

NSData and UIImage

I didn’t try UIImageJPEGRepresentation() before, but UIImagePNGRepresentation works fine for me, and conversion between NSData and UIImage is dead simple: NSData *imageData = UIImagePNGRepresentation(image); UIImage *image=[UIImage imageWithData:imageData];

Print the size (megabytes) of Data in Swift

Use yourData.count and divide by 1024 * 1024. Using Alexanders excellent suggestion: func stackOverflowAnswer() { if let data = #imageLiteral(resourceName: “VanGogh.jpg”).pngData() { print(“There were \(data.count) bytes”) let bcf = ByteCountFormatter() bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only bcf.countStyle = .file let string = bcf.string(fromByteCount: Int64(data.count)) print(“formatted result: \(string)”) } } With … Read more

Finding image type from NSData or UIImage

If you have NSData for the image file, then you can guess at the content type by looking at the first byte: + (NSString *)contentTypeForImageData:(NSData *)data { uint8_t c; [data getBytes:&c length:1]; switch (c) { case 0xFF: return @”image/jpeg”; case 0x89: return @”image/png”; case 0x47: return @”image/gif”; case 0x49: case 0x4D: return @”image/tiff”; } return … Read more

How to compress/resize image on iOS before uploading to a server?

This snippet will resize the image: UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); The variable newSize is a CGSize and can be defined like so: CGSize newSize = CGSizeMake(100.0f, 100.0f);

NSData to [Uint8] in Swift

You can avoid first initialising the array to placeholder values, if you go through pointers in a slightly convoluted manner, or via the new Array constructor introduced in Swift 3: Swift 3 let data = “foo”.data(using: .utf8)! // new constructor: let array = [UInt8](data) // …or old style through pointers: let array = data.withUnsafeBytes { … Read more

Best way to serialize an NSData into a hexadeximal string

This is a category applied to NSData that I wrote. It returns a hexadecimal NSString representing the NSData, where the data can be any length. Returns an empty string if NSData is empty. NSData+Conversion.h #import <Foundation/Foundation.h> @interface NSData (NSData_Conversion) #pragma mark – String Conversion – (NSString *)hexadecimalString; @end NSData+Conversion.m #import “NSData+Conversion.h” @implementation NSData (NSData_Conversion) #pragma … Read more

Convert NSData to String?

Objective-C You can use (see NSString Class Reference) – (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding Example: NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; Remark: Please notice the NSData value must be valid for the encoding specified (UTF-8 in the example above), otherwise nil will be returned: Returns nil if the initialization fails for some reason (for example if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)