I'm using tableview to display different pdfs locally. For example;
pdf1 -> "1.pdf"
pdf2 -> "2.pdf"
pdf3 -> "3.pdf"
In my first click, there is no problem but when I tap again it loads different pdf. Ex: I tap pdf2 but it loads pdf1.
Can you help me in this issue, I'm new to swift..
You can easily do it with ;
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSURLCache.sharedURLCache().diskCapacity = 0
NSURLCache.sharedURLCache().memoryCapacity = 0
Also use like this;
let url = NSURL(string: "http://www.web.com")
let url_request = NSURLRequest(URL: url,
cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 5.0)
let webView = UIWebView()
webView.loadRequest(url_request)
Thanks
Related
I have an api in which I get a link for request. In order to get a picture, I need to make a request in response to which I receive image data. How can I make getting asynchronous images? I find a lot of libraries and videos in youtube where this make with image link. But how i can do it in my case?
let token_type = KeychainWrapper.standard.string(forKey: "token_type")
let access_token = KeychainWrapper.standard.string(forKey: "access_token")
let headers:HTTPHeaders? = ["Authorization": "\(token_type ?? "") \(access_token ?? "")"]
Alamofire.request(cellInfo.imageLink ?? "", method: .get, headers: headers).responseImage { response in
if let image = response.result.value {
cell.firstTypeImageView.image = image
}
}
The issue with your example is that if the cell is reused by the time the image is retrieved, you’ll be updating the wrong row with this image. If updating the image view, it’s better to use the UIImageView extension. You enjoy several benefits with table view cells when you do this because it will automatically cancel the prior request for this cell. This means that:
It eliminates the problem of scrolling quickly having the image view in a reused cell flickering as images are retrieved for rows in the table.
It also means that you don’t have to worry about the current cell getting backlogged behind requests for cells that are no longer visible.
So, you might do something like:
if let imageLink = cellInfo.imageLink, let url = URL(string: imageLink) {
var request = URLRequest(url: url)
let authValue = "\(token_type ?? "") \(access_token ?? "")"
request.setValue(authValue, forHTTPHeaderField: "Authorization")
cell.firstTypeImageView.af_setImage(withURLRequest: request)
}
Or maybe you have a placeholder image, e.g. blank.png, then you’d do:
cell.firstTypeImageView.af_setImage(withURLRequest: request, placeholderImage: UIImage(named: "blank"))
Note, if your images are much larger than the image view in which you’re presenting them, you can see some stuttering in the tableview as the OS struggles to deal with such large assets. In that scenario, you can add a resizing filter:
let filter = AspectScaledToFillSizeFilter(size: cell.firstTypeImageView.frame.size) // or use whatever size appropriate
cell.firstTypeImageView.af_setImage(withURLRequest: request, placeholderImage: UIImage(named: "blank"), filter: filter)
I have a GitHub repository that allows the user to save a paginated PDF on Mac from some HTML by loading it into a WebView and using an NSPrintOperation (specifically, an NSPrintSaveJob with showsPrintPanel set to false) lets the user save that PDF to any location on their Mac with a save panel similar to the default NSSavePanel. However, I'm experimenting with the code and I'd like to instead save the created PDF to a particular folder (/Users/owlswipe/Downloads/) without the save panel.
My code to save a PDF from a WebView (with a save panel) is currently this:
let printOpts: [String : AnyObject] = [NSPrintJobDisposition:NSPrintSaveJob as AnyObject]
let printInfo: NSPrintInfo = NSPrintInfo(dictionary: printOpts)
printInfo.paperSize = NSMakeSize(595.22, 841.85)
let printOp: NSPrintOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
printOp.showsPrintPanel = false
printOp.showsProgressPanel = false
printOp.run()
How can I adapt that code to save the PDF to a preset folder instead of to the user's choice of folders from a save panel?
This piece of code is working for me in Swift 4/Cocoa to accomplish what you want, however there's a bit more code in there as it is rendering the contents of a WKWebView into 8.5" x 11" pages for a PDF.
So for your app, there would be appropriate adjustments for your content stream / print object, but the configuration of the print operation will be the same to get the "no dialog" results you're wanting.
However, to test it out you could just dump your string into the webview and use the code as is. Generated files appear in the root of your "user/documents" directory.
static func createPDF(htmlString: String, streamId: String = "someStream") {
let webView = WebView()
webView.mainFrame.loadHTMLString(htmlString, baseURL: nil)
let when = DispatchTime.now() + 1
DispatchQueue.main.asyncAfter(deadline: when) {
let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let directoryURLStr = directoryURL.absoluteString+"\(streamId).pdf"
let outputFilePath = URL(string: directoryURLStr)
let printOpts: [NSPrintInfo.AttributeKey : Any] = [
NSPrintInfo.AttributeKey.jobDisposition : NSPrintInfo.JobDisposition.save,
NSPrintInfo.AttributeKey.jobSavingURL : outputFilePath
]
let printInfo: NSPrintInfo = NSPrintInfo(dictionary: printOpts)
let baseMargin: CGFloat = 9.0; // .125"
printInfo.paperSize = NSMakeSize(612, 792); // 8.5" x 11/2"
printInfo.topMargin = baseMargin
printInfo.leftMargin = baseMargin
printInfo.rightMargin = baseMargin
printInfo.bottomMargin = baseMargin
let printOp: NSPrintOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
printOp.showsPrintPanel = false
printOp.showsProgressPanel = false
printOp.run()
Swift.print("Document complete: \(outputFilePath!.absoluteString)")
}
}
I am creating iMessage app and trying to send audio or video file to other user.
Video file works and looks fine but its not working as expected with audio file.
My current code is:
let destinationFilename = mp3FileNames[i]
let destinationURL = docDirectoryURL.appendingPathComponent(destinationFilename)
if let conversation = activeConversation {
let layout = MSMessageTemplateLayout()
layout.image = UIImage.init(named: "audio-x-generic-icon")
layout.mediaFileURL = destinationURL
layout.caption = selectedSongObj.name
let message = MSMessage()
message.layout = layout
message.url = URL(string: "emptyURL")
conversation.insert(message, completionHandler: nil)
return
}
Looks like layout.mediaFileURL = destinationURL is not adding any file into message.
And when I try to send file with above code.It looks like shown below:
It looks fine but there is no audio to play but if I try this way:
let destinationFilename = mp3FileNames[i]
let destinationURL = docDirectoryURL.appendingPathComponent(destinationFilename)
if let conversation = activeConversation {
conversation.insertAttachment(destinationURL!, withAlternateFilename: nil, completionHandler: nil)
return
}
And result with above code is:
I can play audio for that message because it's there. But problem with that message is I can not attach any image or caption with it.
How can I attach image and audio file into same message.
And if possible instead of image can I add GIF?
Any help would be much appreciated, Thank you.
Not necessary to use GIF, iMessage extensions supports also PNGand JPEG image formats. Recommended image size is 300x300 points at #3x scale.
If the MSMessageTemplateLayout's image property has a non-nil value then
mediaFileURL property is ignored. So you can't send an image and an audio file at the same time. Docs
When I try to share a link along with a picture, at the same time, via FBSDKSharePhoto, only the photo gets shared. No link appears.
Here is the code I use:
let photo : FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = self.scaledImage
photo.userGenerated = true
let content : FBSDKSharePhotoContent = FBSDKSharePhotoContent()
content.contentURL = NSURL(string: self.short_string)
content.photos = [photo]
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
And a picture of the resulting dialog when I push the button: http://imgur.com/mZ483i9 (also attached)
You can see the picture gets attached to the post, but there is no link. I know all of the URL/Photo variables work fine because they are used elsewhere.
If I switch the sharing method to FBSDKShareLinkContent then content.contentURL works fine, but I cannot post the picture this way because the picture is not hosted on the web, which is why I am using FBSDKSharePhotoContent. (The picture is taken within the app.)
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: self.short_string)
content.contentTitle = self.title_text
content.contentDescription = self.desc_text
content.imageURL = NSURL(string: "http://randomimage.jpg/")
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
So I know my link is structured properly according to the contentURL protocol, and FBSDKSharePhotoContent conforms to the same functions as FBSDKShareLinkContent. No clue why it won't work.
Due to Facebook rules you can`t set non user generated text.
I can't for the life of me figure out a way to play an MP4 that takes up the entire background in a UIViewController.
So far I have the following, which doesn't even play the video at all.
I can confirm that the bokeh.mp4 video exists because if I change the file to something else then it throws an error that it's missing.
override func viewDidAppear(animated: Bool) {
let filePath = NSBundle.mainBundle().pathForResource("bokeh", ofType: "mp4")
self.moviePlayerController.contentURL = NSURL(string: filePath)
self.moviePlayerController.prepareToPlay()
self.moviePlayerController.repeatMode = .One
self.moviePlayerController.controlStyle = .Embedded
self.view.addSubview(self.moviePlayerController.view)
}
I get an error in the console:
2014-06-22 21:22:42.347 MoviePlayer[26655:60b] _itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
I've also tried adding a UIView that takes up the entire screen and adding the player to that view, but it's the same problem. It doesn't start playing.
I'm trying to achieve the same effect that Vine has when you first load up the application where it has the video playing in the background.
So this was blindly annoying:
All I did was change:
self.moviePlayerController.contentURL = NSURL(string: filePath)
TO:
self.moviePlayerController.contentURL = NSURL.fileURLWithPath(filePath)