Error trying to upload an image to CloudKit using Swift3 - ios

I'm running into a compiler error when using the following code:
func saveImageToDisk() {
let imageData = UIImagePNGRepresentation(imageView.image!)!
let fileName = getDocumentsDirectory().appendingPathComponent("image.png")
imageData.writeToFile(fileName, atomically: true)
}
The error is: Value of type 'Data' has no member 'writeToFile'
Could this be a compiler error, or something I'm missing? Thanks

SE-0005 proposed a better translation of Objective-C APIs into Swift and that affected NSData (or just Data now). Instead of writeToFile you'll have to use write(to:options:) (or even just write(to:)). Here is the documentation for the updated method.

Related

Build time is too long in Xcode 8.3 with swift 3 in particular file

Im using Xcode 8.3 with Swift 3. I have written one method named pdfFromData(data:) to form the pdf document from the Data, whenever I build my project its not getting build due to this method, means the compiler is got stopped/hanged when it start compile particular file where I coded pdfFromData(data:) method(In Xcode 8.2 with Swift 3 it worked fine). Whenever i comment this method and build means everything working fine.
func pdfFromData(data: Data) -> CGPDFDocument? { // Form pdf document from the data.
if let pdfData = data as? CFData {
if let provider = CGDataProvider(data: pdfData) {
let pdfDocument = CGPDFDocument(provider)
return pdfDocument
}
}
return nil
}
What's wrong with this method?. I want to build my project with this method as well. Thanks in advance.
I tried debugging your issue. This is what I found out:
if let pdfData = data as? CFData {
}
The above line for casting object of type Data to CFData is where it's taking too much time to build.
Replacing that with the following piece of code significantly reduces your build time.
let pdfNsData: NSData = NSData(data: data) // convert `Data` to `NSData`
if let cfPdfData: CFData = pdfNsData as? CFData {
// cast `NSData` to `CFData`
}
NSData and CFData are toll-free bridged.
Please let me know if there's any doubt

Play audio file using EZAudio Framework in Swift

Hi I am trying to convert following Objective-C code to Swift:
EZAudioFile *audioFile = [EZAudioFile audioFileWithURL:NSURL]; //required type NSURL
[self.player playAudioFile:audioFile];
But I am unable to make it work.
let audioFile = EZAudioFile.url(EZAudioFile) //required type EZAudioFile, so I am unable to pass the NSURL of the audio file here.
player.play()
Error is : Cannot convert value of type 'NSURL' to expected argument type 'EZAudioFile'
The above objective-C code is referenced from here : EZAudio Example:Record File
I didn't test, so I may be wrong, but I think you're not using the right syntax: EZAudioFile.url(EZAudioFile) does not call the initializer you're thinking of.
I see in the EZAudioFile source that there's indeed an initializer for an audio file URL:
+ (instancetype)audioFileWithURL:(NSURL *)url
So my guess is that the syntax in Swift should rather be:
let audioFile = EZAudioFile(audioFileWithURL: yourURL)
Also, it seems that it is only a wrapper for the normal URL init, which should be something like:
let audioFile = EZAudioFile(URL: yourURL)

exc-bad-instruction code=i386_invop

I'm getting this error in latest version of xcode using swift 2
on line
let s = linkTxt.text
Text in linkTxt appears by button "pasteFromClipBoard"
let s = linkTxt.text
let u = NSURL(string: s!)
let file = u?.lastPathComponent
What is the reason of it and how to fix it?
Update:
the problem appears in function saveData() which calls when file downloading is finished. It calls from NSURLSessionDataTask function. More interesting, that in start-downloading-button there are the same lines where filename is generating and there is no such error on it. I fixed these issues by declaring variables, writing text's values into them and use these variables in saveData() except textObject.text; I had to delete lines with NSUserDefaults from saveData() too because I got the same error. Did understand nothing >_<
Update 2:
It's really a bug. I've deleted this line and wrote again - problem fixed
linkTxt.txt is returning nil and NSURL(string: s!) will try to forcefully unwrap it.
let s = linkTxt.text
if let s = linkTxt.txt {
let u = NSURL(string: s!)
let file = u?.lastPathComponent
}

Got some errors after update Xcode 7 with Swift 2.0?

Today i updated my xcode to version 7 with swift 2.0.
Then i got so many errors in my production app, But i already fixed most of it by myself.
The problem is some of it i don't know how to fix.
So the images below are errors that i could not fix it myself.
If anyone knows how to fix it please help.
Thanks!
As explain in the comment, you simply have to rewrite those lines by checking the method signature :
1.
// Use the NSURL methods instead of String ones
let path = NSURL(fileURLWithPath: documentsFolder).URLByAppendingPathComponent("baseDeck.sqlite").path!
2.
// Make sure the productId is a String
request = SKProductsRequest(productIdentifiers: [productId])
3.
// Be careful to parameters, they are optionals
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) { (data: CMAccelerometerData?, error: NSError?) -> Void in
//...
}
4.
// Be careful too, the invalidProductIdentifiers method return an array of string whereas the response.products an array of SKProduct
let products = responses.products.filter { productIndentifiers.contains($0.productIdentifier) }
I hope it'll help you.

UIImage AsPNG and AsJPEG fails

I'm using MonoTouch and I have a UIImage (displayed in a UIImageView and it looks good) and I'm trying to convert it to NSData, but AsJPEG and AsPNG returns null. What can be the problem?
My code looks like this:
NSError err;
NSData imageData = CroppedImageView.Image.AsJPEG(); // imageData is null!
if (!imageData.Save ("tmp.png", true, out err)) {
Console.WriteLine("Saving of file failed: " + err.Description);
}
The AsJPEG method calls UIImageJPEGRepresentation and its return value is documented as:
A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
The is similar to many API in iOS (and OSX) where exception are not commonly used (and null is used to report some kind of error).
Anyway you should check your image dimensions and properties - they might give you an hint at something that would not translate into a JPEG bitmap.
Also since the NSData can represent a very large amount of memory you should try to limit it's life, e.g.:
using (NSData imageData = CroppedImageView.Image.AsJPEG ()) {
NSError err;
if (!imageData.Save ("tmp.jpg", true, out err)) {
Console.WriteLine("Saving of file failed: " + err.Description);
}
}
It looks like you are writing to a file in the current directory of the app, this is readonly.
You should use:
var path = System.IO.Path.GetTempFilename();
or
var path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "tmp.png");
Like you would do on other platforms, and use a file from there.
You can also use Environment.SpecialFolder.MyDocuments.
The AsJPEG returned null because the image size was too big (it was taken with an iPhone 5). After I Scaled it down by 2, it generates the data properly.

Resources