Convert UIImage to NSData and convert back to UIImage in Swift?
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()
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()
Try one of the following, depending on your image format: UIImageJPEGRepresentation Returns the data for the specified image in JPEG format. NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality ); UIImagePNGRepresentation Returns the data for the specified image in PNG format NSData * UIImagePNGRepresentation ( UIImage *image ); Here the docs. EDIT: if you want … Read more
This is the implemented code needed: in Swift 3.0: var dataString = String(data: fooData, encoding: String.Encoding.utf8) or just var dataString = String(data: fooData, encoding: .utf8) Older swift version: in Swift 2.0: import Foundation var dataString = String(data: fooData, encoding: NSUTF8StringEncoding) in Swift 1.0: var dataString = NSString(data: fooData, encoding:NSUTF8StringEncoding)
In Swift 3 let data = string.data(using: .utf8) In Swift 2 (or if you already have a NSString instance) let data = string.dataUsingEncoding(NSUTF8StringEncoding) In Swift 1 (or if you have a swift String): let data = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding) Also note that data is an Optional<NSData> (since the conversion might fail), so you’ll need to … Read more
If anyone is looking for a way to do this in Swift: Swift 3 introduces the Data type, with value semantics. To convert the deviceToken to a String, you can do as follows: func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let token = deviceToken.map { String(format: “%02.2hhx”, $0) }.joined() print(token) } Old answer using … Read more
Swift First we need to have image’s NSData //Use image name from bundle to create NSData let image : UIImage = UIImage(named:”imageNameHere”)! //Now use image to create into NSData format let imageData:NSData = UIImagePNGRepresentation(image)! //OR next possibility //Use image’s path to create NSData let url:NSURL = NSURL(string : “urlHere”)! //Now use image to create into … Read more
If the data is not null-terminated, you should use -initWithData:encoding: NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end. NSString* newStr = [NSString stringWithUTF8String:[theData bytes]]; (Note that if the input is not properly UTF-8-encoded, you will get nil.) Swift … Read more
NSString* str = @”teststring”; NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];