I have a line in a code which became deprecated, there are suggestions in XCode what to replace it with, but I can't get my head around the difference, these are my three lines which worked:
let path = NSBundle.mainBundle().pathForResource("example", ofType: ".p12")
let pkcs12Data = NSData.dataWithContentsOfMappedFile(path!)
let cf: CFDataRef = pkcs12Data as! CFDataRef
Now according to warning and suggestions I changed my code to:
let path = NSBundle.mainBundle().pathForResource("example", ofType: ".p12")
let pkcs12Data = NSData(contentsOfFile: path!)
let cf: CFDataRef = pkcs12Data as! CFDataRef
Which gives me an error:
EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)
A slightly safer version:
guard
let url = NSBundle.mainBundle().URLForResource("example", withExtension: ".p12"),
let data = NSData(contentsOfURL: url)
else { // Do something because you couldn't get the file or convert it to NSData }
let dataPtr = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(data.bytes), data.length)
Note, using file based URLs instead of string paths.
When deciding which routines to call, choose ones that let you specify paths using NSURL objects over those that specify paths using strings. Most of the URL-based routines were introduced in OS X v10.6 and later and were designed from the beginning to take advantage of technologies like Grand Central Dispatch. This gives your code an immediate advantage on multicore computers while not requiring you to do much work.
From File System Programming Guide
In Swift 4, however, you should do it this way:
Xcode 9.2 seems to treat the NSData as Data automatically when the two optionals are put in the same guard-let clause.
I have to put the two optionals in separate guard-let clause, as bellow:
// guard let url = Bundle.main.url(forResource: "example", withExtension: ".p12"),
// let data = NSData(contentsOf: url) else {
// return
// }
guard let url = Bundle.main.url(forResource: "example", withExtension: ".p12") else {
return
}
guard let data = NSData(contentsOf: url) else {
return
}
let bytes = data.bytes.assumingMemoryBound(to: UInt8.self)
let cfData = CFDataCreate(kCFAllocatorDefault, bytes, data.length) // CFData object you want
#Abizern’s answer works, but using CFDataCreateWithBytesNoCopy instead of CFDataCreate is more effective.
Related
ok guys can I get a little help with my code. When running the app I get an error is there any way to fix this problem?
let fileUrl = dict["fileUrl"]as! String
let url = NSURL(string: fileUrl)
let data = NSData(contentsOf: url! as URL!)
let picture = UIImage(data: data! as Data!)
let photo = JSQPhotoMediaItem(image: picture)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
Image here
Here I see 4 big fat problems with your code.
You are force casting the value of fileUrl of the dictionary dict to String. If your dictionary doesn't have the value for fileUrl, or if it's not castable to string, your code will crash. You should change that to optional cast like:
if let fileUrl = dict["fileUrl"] as? String
{
//your code if you have fileUrl
}
When creating the url to the file, you are using the wrong initialization method, you should be using this:
let url = URL(fileURLWithPath: fileUrl)
After you have the url to the file, you should also check if you have the data of the file, because contentsOfFile: initializer of the NSData returns the optional object, which may be nil, so another if check:
if let data = NSData(contentsOf: url) {\\ code with the data}
init?(data: Data) initializer of the UIImage also returns optional object, so if the required by latter code, you should also check if you have the image or nil with if statement.
The result code should be something like:
if let fileUrl = dict["fileUrl"] as? String {
let url = URL(fileURLWithPath: fileUrl)
if let data = NSData(contentsOf: url) {
let image = UIImage(data: data as Data) // you can cast NSData to Data without force or optional casting
let photo = JSQPhotoMediaItem(image: image)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
}
}
Hope this helps.
Replace the first line of code with this line for optional binding check :-
guard let fileUrl = dict["fileUrl"] as! String else {return}
Yo should do validation in cases where the variable may be nil, the following is an example:
if let fileUrl = dict["fileUrl"] as? String {
let url = URL(string: fileUrl)
do {
let data = try Data(contentsOf: url!)
let picture = UIImage(data: data)
let photo = JSQPhotoMediaItem(image: picture)
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, media: photo))
} catch {
}
}
New to iOS/Swift. I am trying to migrate a project (that simply fetches contents from a URL via the NSData init() method) from Swift 2 to Swift 3. The original code looks like this:
let loadedImageData = NSData(contentsOfURL: imageURL)
dispatch_async(dispatch_get_main_queue()) {
if imageURL == user.profileImageURL {
if let imageData = loadedImageData {
self.profileImageView?.image = UIImage(data: imageData)
}
}
}
Swift 3 migration:
let loadedImageData = NSData(contentsOf: imageURL as URL)
DispatchQueue.main.async {
if imageURL == user.profileImageURL {
if let imageData = loadedImageData {
self.profileImageView?.image = UIImage(data: imageData as Data)
}
}
}
I am not sure as to why we need to cast the NSData return value as a URL and then cast that return again to a Data type while loading the image within Swift 3. We are assigning the raw data to a variable loadedImageData in both the version. Why the casting then? It seems that the UIImage init() method needs a data object within Swift 3. However for Swift 2 there is no casting for the same. Why is that?
Thanks for the help.
The migration consists of some changes in those methods' signatures, namely, the types they accept.
In Swift 2, NSData(contentsOfURL:) and UIImage(data:) take NSURL and NSData, respectively.
Currently, they have been changed to NSData(contentsOf:) and UIImage(data:) that accept, respectively, URL (struct) and Data (instead of NSData); as a result, the casts are necessary unless you constructed your URL from type URL instead of NSURL.
You could use, instead, Data(contentsOf: URL) to avoid the cast as well.
Using this code, I extract an image from a Share Extension and I write it to a directory I created in an App Group.
let content = self.extensionContext!.inputItems[0] as! NSExtensionItem
let contentType = kUTTypeImage as String
for attachment in content.attachments as! [NSItemProvider] {
if attachment.hasItemConformingToTypeIdentifier(contentType) {
attachment.loadItem(forTypeIdentifier: contentType, options: nil) { data, error in
// from here
if error == nil {
let url = data as! NSURL
let originalFileName = url.lastPathComponent
if let imageData = NSData(contentsOf: url as URL) {
let img = UIImage(data:imageData as Data)
if let data = UIImagePNGRepresentation(img!) {
// write, etc.
}
}
}
}
Anything is working fine.
What I'd like to know is if it is possible to reduce some code: in particular, after if error == nil, I:
cast data to NSURL;
use NSURL to get a NSData;
use NSData to get a UIImage;
use UIImage to get a UIImagePNGRepresentation;
Aside from avoiding the creation of the imageData variable, isn't there a way to (safely) achieve the same goal with fewer steps?
First of all you need to use native Data and URL instead of NSData & NSURL also if you want to write file in DocumentDirectory then you can directly use that imageData no need to make UIImage object from it and then convert it to data using UIImagePNGRepresentation.
if let url = data as? URL, error == nil {
let originalFileName = url.lastPathComponent
if let imageData = try? Data(contentsOf: data) {
// write, etc.
var destinationURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
destinationURL.appendPathComponent("fileName.png")
try? imageData.write(to: destinationURL)
}
}
I'm writing an iOS application using Swift 2 and I would like to save profile picture of an account locally in a Realm database. I can't find any documentation or people talking about that.
Is it possible? And how?
May be is it bad to do that?
You can store images as NSData. Given you have the URL of an image, which you want to store locally, here is a code snippet how that can be achieved.
class MyImageBlob {
var data: NSData?
}
// Working Example
let url = NSURL(string: "http://images.apple.com/v/home/cb/images/home_evergreen_hero_iphone_medium.jpg")!
if let imgData = NSData(contentsOfURL: url) {
var myblob = MyImageBlob()
myblob.data = imgData
let realm = try! Realm()
try! realm.write {
realm.add(myblob)
}
}
May be it is a bad idea to do that?
The rule is simple:
If the images are small in size, the number of images is small and they are rarely changed, you can stick with storing them in the database.
If there is a bunch images, you are better off writing them directly to the file system and just storing the path of the image in the database.
Here is how that can be done:
class MyImageStorage{
var imagePath: NSString?
}
let url = NSURL(string: "http://images.apple.com/v/home/cb/images/home_evergreen_hero_iphone_medium.jpg")!
if let imgData = NSData(contentsOfURL: url) {
// Storing image in documents folder (Swift 2.0+)
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let writePath = documentsPath?.stringByAppendingPathComponent("myimage.jpg")
imgData.writeToFile(writePath, atomically: true)
var mystorage = MyImageStorage()
mystorage.imagePath = writePath
let realm = try! Realm()
try! realm.write {
realm.add(mystorage)
}
}
Please note: Both code samples are not reliable methods for downloading images since there are many pitfalls. In real world apps / in production, I'd suggest to use a library intended for this purpose like AFNetworking or AlamofireImage.
ProblemSlover's solution updated for Swift 2.2:
Important Note: the only change is that now stringByAppendingPathComponent has been removed as of Swift 2.1, so you will get an error saying:
stringByAppendingPathComponent is unavailable: use URLByAppendingPathComponent on NSURL instead.
Please keep in mind that while it may be annoying, it has been removed for a reason and you should follow apple's recommendation to use NSURL instead. BUT if you wish to proceed with this solution, the fix for Swift 2.1+ is to explicitly cast documentsPath to NSString:
class MyImageStorage{
var imagePath: NSString?
}
let url = NSURL(string: "http://images.apple.com/v/home/cb/images/home_evergreen_hero_iphone_medium.jpg")!
if let imgData = NSData(contentsOfURL: url) {
//Explicitly cast to NSString
let documentsPath = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString)
let writePath = documentsPath.stringByAppendingPathComponent("myimage.jpg")
imgData.writeToFile(writePath, atomically: true)
var mystorage = MyImageStorage()
mystorage.imagePath = writePath
let realm = try! Realm()
try! realm.write {
realm.add(mystorage)
}
}
Now apple have handily gotten rid of the NSString and String automatic compatibility, I'm having a bit of a nightmare going between the two. I'm getting a couple of NSStrings out of a dictionary and I can't convert them to regular Strings...
I've tried:
let fileNameString: String = String(format: "%#", filename!)
let fileNameString: String = (filename as! String)
let fileNameString = filename as? String
let fileNameString = (filename as? String) ?? ""
if let fileNameString = filename as? String {
println("\(fileNameString)")
}
But all produce the error.
I've broken in at the point of conversion and can see neither NSStrings are nil:
But no joy with either. Getting Thread 1: EXC_BAD_ACCESS (code=1, address=0x20) . Am I missing something obvious here?
Even just trying to print the NSString filename before conversion causes the same error..
Posting the code prior to conversion attempt to see if that has anything to do with it...
// First we create a head request as the info I need is in the headers
var newRequest: NSMutableURLRequest = NSMutableURLRequest(URL: request.URL!)
newRequest.HTTPMethod = "HEAD"
var response: NSURLResponse?
NSURLConnection.sendSynchronousRequest(newRequest, returningResponse: &response, error: nil)
// Unwrap response as httpResponse in order to access allHeaderFields
if let httpResponse = response as? NSHTTPURLResponse {
let headerString = "sfn-Document-Filename"
let headerNSString = headerString as NSString
let filetypeString = "Content-Type"
let filetypeNSString = filetypeString as NSString
// This is a dictionary where the keys are NSCFStrings
// (NSStrings, hence creating the NSStrings above)
var allHeaders = httpResponse.allHeaderFields
// Getting the filename out here only works with as? NSString. as? String creates the same error as converting.
let filename = allHeaders[headerNSString] as? NSString
// This is a string which contains the type as 'application/pdf' for example. We only need the part after the /.
// Again, trying to get this out as a String fails
let typeString = allHeaders[filetypeNSString] as? NSString
var typeArray = typeString?.componentsSeparatedByString("/") as! [NSString]
let filetype = typeArray[1]
}
If this were an NSString, then all you’d need to do is filename as String (no !). But it sounds like the problem is your filename, of optional type NSString?, is nil. (option-click filename to confirm its type)
If there’s a reasonable default (say, an empty string), try
let fileNameString = (filename as? String) ?? ""
Or if you need to handle the nil with specific code:
if let fileNameString = filename as? String {
// use fileNameString, which will be unwrapped and of type String
}
else {
// log error or similar
}
Or, if you want to defer unwrapping, but want to change the type inside the possible value, you can do
let fileNameString = filename as? String
// which is a less long-winded way of saying
let fileNameString = filename.map { $0 as String }
Generally speaking, you should try and cut down on the ! usage, since it leads to problems like this. ! is only for those times when you know from the code that the value absolutely positively cannot be nil.
edit: based on your sample code, try the following:
let url = NSURL(string: "http://www.google.com")
let request = url.map { NSMutableURLRequest(URL: $0) }
request?.HTTPMethod = "HEAD"
let response: NSHTTPURLResponse? = request.flatMap {
var response: NSURLResponse?
NSURLConnection.sendSynchronousRequest($0, returningResponse: &response, error: nil)
return response as? NSHTTPURLResponse
}
let headers = response?.allHeaderFields as? [String:String]
// google.com has no such header but still...
let filename = headers?["sfn-Document-Filename"]
// bear in mind someArray[1] will also crash if there's no such entry,
// first/last are better if that's what you want
let type = headers?["Content-Type"]?
.componentsSeparatedByString(";").first?
.componentsSeparatedByString("/").last
Everything here is optional, but safe, so you can test at various points for nil for logging/error reporting purposes.