I'm working on a share extension for my app, to be able to use it for sharing images directly from the library. I have a view controller and layout set up, but I'm struggling with getting the selected image to show in the UIImageView inside the view controller.
For now, my getImage() is as follows:
func getImage() {
if let inputItem = extensionContext!.inputItems.first as? NSExtensionItem {
if let itemProvider = inputItem.attachments?.first as? NSItemProvider {
itemProvider.loadItem(forTypeIdentifier: kUTTypeJPEG as String) { [unowned self] (imageData, error) in
if let item = imageData as? Data {
self.imageView.image = UIImage(data: item)
}
}
}
}
}
...but the image is not loading. What am I doing wrong here?
Found the problem. Turned out I needed to do a type identifier conformance check first:
func getImage() {
if let inputItem = extensionContext!.inputItems.first as? NSExtensionItem {
if let itemProvider = inputItem.attachments?.first as? NSItemProvider {
// This line was missing
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeJPEG as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeJPEG as String) { [unowned self] (imageData, error) in
if let item = imageData as? Data {
self.imageView.image = UIImage(data: item)
}
}
}
}
}
}
Related
I have tableview with label, imageView (for image, gif & video thumbnail). I am sure that doing something wrong and I can't handle its completion handler due to which the app is hanged and gets stuck for a long time.
My model is like,
struct PostiisCollection {
var id :String?
var userID: String?
var leadDetails : NSDictionary?
var company: NSDictionary?
var content: String?
init(Doc: DocumentSnapshot) {
self.id = Doc.documentID
self.userID = Doc.get("userID") as? String ?? ""
self.leadDetails = Doc.get("postiiDetails") as? NSDictionary
self.company = Doc.get("company") as? NSDictionary
self.content = Doc.get("content") as? String ?? ""
}
}
I wrote in my view controller for fetch this,
var postiisCollectionDetails = [PostiisCollection]()
override func viewDidLoad() {
super.viewDidLoad()
let docRef = Firestore.firestore().collection("PostiisCollection").whereField("accessType", isEqualTo: "all_access")
docRef.getDocuments { (querysnapshot, error) in
if let doc = querysnapshot?.documents, !doc.isEmpty {
print("Document is present.")
for document in querysnapshot!.documents {
_ = document.documentID
if let compCode = document.get("company") as? NSDictionary {
do {
let jsonData = try JSONSerialization.data(withJSONObject: compCode)
let companyPost: Company = try! JSONDecoder().decode(Company.self, from: jsonData)
if companyPost.companyCode == AuthService.instance.companyId ?? ""{
print(AuthService.instance.companyId ?? "")
if (document.get("postiiDetails") as? NSDictionary) != nil {
let commentItem = PostiisCollection(Doc: document)
self.postiisCollectionDetails.append(commentItem)
}
}
} catch {
print(error.localizedDescription)
}
DispatchQueue.main.async {
self.tableView.isHidden = false
self.tableView.reloadData()
}
}
}
}
}
}
I need to check for the index path with image view is either image or gif or video with different parameters, I tried with tableview delegate and datasource method by,
extension AllAccessPostiiVC: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postiisCollectionDetails.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AllAccessCell", for: indexPath)
let label1 = cell.viewWithTag(1) as? UILabel
let imagePointer = cell.viewWithTag(3) as? UIImageView
let getGif = arrPostiisCollectionFilter[indexPath.row].leadDetails?.value(forKey: "gif") as? NSArray
let getPhoto = arrPostiisCollectionFilter[indexPath.row].leadDetails?.value(forKey: "photo") as? NSArray
let getVideo = arrPostiisCollectionFilter[indexPath.row].leadDetails?.value(forKey: "video") as? NSArray
label1?.text = "\(arrPostiisCollectionFilter[indexPath.row].leadDetails?.value(forKey: "title"))"
if getGif != nil {
let arrGif = getGif?.value(forKey: "gifUrl") as! [String]
print(arrGif[0])
let gifURL : String = "\(arrGif[0])"
let imageURL = UIImage.gifImageWithURL(gifURL)
imagePointer?.image = imageURL
playButton?.isHidden = true
}
if getPhoto != nil {
let arrPhoto = getPhoto?.value(forKey: "photoUrl") as! [String]
print(arrPhoto[0])
let storageRef = Storage.storage().reference(forURL: arrPhoto[0])
storageRef.downloadURL(completion: { (url, error) in
do {
let data = try Data(contentsOf: url!)
let image = UIImage(data: data as Data)
DispatchQueue.main.async {
imagePointer?.image = image
playButton?.isHidden = true
}
} catch {
print(error)
}
})
}
if getVideo != nil {
let arrVideo = getVideo?.value(forKey: "videoUrl") as! [String]
let videoURL = URL(string: arrVideo[0])
let asset = AVAsset(url:videoURL!)
if let videoThumbnail = asset.videoThumbnail{
SVProgressHUD.dismiss()
imagePointer?.image = videoThumbnail
playButton?.isHidden = false
}
}
}
}
If I run, the app hangs in this page and data load time is getting more, some cases the preview image is wrongly displayed and not able to handle its completion
As others have mentioned in the comments, you are better of not performing the background loading in cellFroRowAtIndexPath.
Instead, it's better practice to add a new method fetchData(), where you perform all the server interaction.
So for example:
// Add instance variables for fast access to data
private var images = [UIImage]()
private var thumbnails = [UIImage]()
private func fetchData(completion: ()->()) {
// Load storage URLs
var storageURLs = ...
// Load data from firebase
let storageRef = Storage.storage().reference(forURL: arrPhoto[0])
storageRef.downloadURL(completion: { (url, error) in
// Parse data and store resulting image in image array
...
// Call completion handler to indicate that loading has finished
completion()
})
}
Now you can call fetchData() whenever you would like to refresh data and call tableview.reloadData() within the completion handler. That of course must be done on the main thread.
This approach simplifies your cellForRowAtIndexPath method.
There you can just say:
imagePointer?.image = ...Correct image from image array...
Without any background loading.
I suggest using below lightweight extension for image downloading from URL
using NSCache
extension UIImageView {
func downloadImage(urlString: String, success: ((_ image: UIImage?) -> Void)? = nil, failure: ((String) -> Void)? = nil) {
let imageCache = NSCache<NSString, UIImage>()
DispatchQueue.main.async {[weak self] in
self?.image = nil
}
if let image = imageCache.object(forKey: urlString as NSString) {
DispatchQueue.main.async {[weak self] in
self?.image = image
}
success?(image)
} else {
guard let url = URL(string: urlString) else {
print("failed to create url")
return
}
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
// response received, now switch back to main queue
DispatchQueue.main.async {[weak self] in
if let error = error {
failure?(error.localizedDescription)
}
else if let data = data, let image = UIImage(data: data) {
imageCache.setObject(image, forKey: url.absoluteString as NSString)
self?.image = image
success?(image)
} else {
failure?("Image not available")
}
}
}
task.resume()
}
}
}
Usage:
let path = "https://i.stack.imgur.com/o5YNI.jpg"
let imageView = UIImageView() // your imageView, which will download image
imageView.downloadImage(urlString: path)
No need to put imageView.downloadImage(urlString: path) in mainQueue, its handled in extension
In your case:
You can implement following logic in cellForRowAt method
if getGif != nil {
let arrGif = getGif?.value(forKey: "gifUrl") as! [String]
let urlString : String = "\(arrGif[0])"
let image = UIImage.gifImageWithURL(urlString)
imagePointer?.image = image
playButton?.isHidden = true
}
else if getPhoto != nil {
let arrPhoto = getPhoto?.value(forKey: "photoUrl") as! [String]
let urlString = Storage.storage().reference(forURL: arrPhoto[0])
imagePointer?.downloadImage(urlString: urlString)
playButton?.isHidden = true
}
elseif getVideo != nil {
let arrVideo = getVideo?.value(forKey: "videoUrl") as! [String]
let urlString = arrVideo[0]
imagePointer?.downloadImage(urlString: urlString)
playButton?.isHidden = false
}
If you have one imageView to reload in tableView for photo, video and gif. then use one image array to store it prior before reloading. So that your main issue of hang or stuck will be resolved. Here the main issue is each time in table view cell collection data is being called and checked while scrolling.
Now I suggest to get all photo, gifs and video (thumbnail) as one single array prior to table view reload try this,
var cacheImages = [UIImage]()
private func fetchData(completionBlock: () -> ()) {
for (index, _) in postiisCollectionDetails.enumerated() {
let getGif = postiisCollectionDetails[index].leadDetails?.value(forKey: "gif") as? NSArray
let getPhoto = postiisCollectionDetails[index].leadDetails?.value(forKey: "photo") as? NSArray
let getVideo = postiisCollectionDetails[index].leadDetails?.value(forKey: "video") as? NSArray
if getGif != nil {
let arrGif = getGif?.value(forKey: "gifUrl") as! [String]
let gifURL : String = "\(arrGif[0])"
self.randomList.append(gifURL)
/////---------------------------
let imageURL = UIImage.gifImageWithURL(gifURL)
self.cacheImages.append(imageURL!)
//////=================
}
else if getVideo != nil {
let arrVideo = getVideo?.value(forKey: "videoUrl") as! [String]
let videoURL: String = "\(arrVideo[0])"
let videoUrl = URL(string: arrVideo[0])
let asset = AVAsset(url:videoUrl!)
if let videoThumbnail = asset.videoThumbnail{
////--------------
self.cacheImages.append(videoThumbnail)
//-----------
}
self.randomList.append(videoURL)
}else if getPhoto != nil {
let arrPhoto = getPhoto?.value(forKey: "photoUrl") as! [String]
let photoURL : String = "\(arrPhoto[0])"
/////---------------------------
let url = URL(string: photoURL)
let data = try? Data(contentsOf: url!)
if let imageData = data {
let image = UIImage(data: imageData)
if image != nil {
self.cacheImages.append(image!)
}
else {
let defaultImage: UIImage = UIImage(named:"edit-user-80")!
self.cacheImages.append(defaultImage)
}
}
//////=================
}
else {
//-----------------
let defaultImage: UIImage = UIImage(named:"edit-user-80")!
self.cacheImages.append(defaultImage)
//--------------------
}
}
completionBlock()
}
After getting all UIImage as array where loop is being called. Now you call this function inside your viewDidLoad. So after all values in images fetched then try to call tableView like this,
override func viewDidLoad() {
self.fetchData {
DispatchQueue.main.async
self.tableView.reloadData()
}
}
}
Now atlast, you may use SDWebImage or any other background image class or download method to call those in tableView cellforRow method,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// your cell idetifier & other stuffs
if getVideo != nil {
imagePointer?.image = cacheImages[indexPath.row]
playButton?.isHidden = false
}else {
imagePointer?.image = cacheImages[indexPath.row]
// or get photo with string via SdWebImage
// imagePointer?.sd_setImage(with: URL(string: photoURL), placeholderImage: UIImage(named: "edit-user-80"))
playButton?.isHidden = true
}
return cell
}
You're handling data in a totally wrong manner. Data(contentsOf: url!) - This is wrong. You should chache the images and should download it to directory. When you convert something into data it takes place into the memory(ram) and it is not good idea when playing with large files. You should use SDWebImage kind of library to set images to imageview.
Second thing if let videoThumbnail = asset.videoThumbnail - This is also wrong. Why you're creating assets and then getting thumbnail from it? You should have separate URL for the thumbnail image for your all videos in the response of the API and then again you can use SDWebImage to load that thumbnail.
You can use SDWebImage for gif as well.
Alternative of SDWebImage is Kingfisher. Just go through both libraries and use whatever suitable for you.
I'm trying to rewrite my image cache to get away from using Alamofire, and in doing so I've run into an error. Previously, my image cache code was:
let imageCache = NSCache<NSString, UIImage>()
extension UIImageView {
func loadImageUsingCacheWithUrlString(_ urlString: String) {
self.image = nil
// Check cache for image first
if let cachedImage = imageCache.object(forKey: urlString as NSString) {
self.image = cachedImage
return
}
// Otherwise fire off a new download
Alamofire.request(urlString)
.responseImage { response in
if let downloadedImage = response.result.value {
// image is here.
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
}
}
}
And an example use is as follows:
navBarCell.avatarImageView.loadImageUsingCacheWithUrlString(avatars[indexPath.row])
Now I've rewritten the cache like so:
class ImageService {
static let cache = NSCache<NSString, UIImage>()
static func downloadImage(url: URL, completion: #escaping (_ image: UIImage?) -> (Void)) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {return}
var downloadedImage: UIImage?
downloadedImage = UIImage(data: data)
if downloadedImage != nil {
cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString)
}
DispatchQueue.main.async {
completion(downloadedImage)
}
}.resume()
}
static func getImage(url: URL, completion: #escaping (_ image: UIImage?) -> (Void)) {
if let image = cache.object(forKey: url.absoluteString as NSString) {
completion(image)
} else {
downloadImage(url: url, completion: completion)
}
}
}
And to use it in the same scenario as above:
if let avatarImageUrlString = self.chatVC?.avatarDictionary.allValues as! [String], let imageUrl = URL(string: avatarImageUrlString) {
ImageService.getImage(url: imageUrl, completion: { (image) -> (Void) in
navBarCell.avatarImageView.image = image
})
}
And I'm getting the error Initializer for conditional binding must have Optional type, not '[String]' on the if let statement.
I'm not sure how to handle this - I need the [String] array but if I'm understanding the error correctly it needs to be an optional instead of force unwrapping with as! [String]? I apologize if its a newbie question but I can't seem to get it working.
Thanks for any guidance!
EDIT:
I've tried putting the let avatarImageUrlString.... outside of the if let:
let avatarImageUrlString = self.chatVC?.avatarDictionary.allValues as! [String]
if let imageUrl = URL(string: avatarImageUrlString) {
ImageService.getImage(url: imageUrl, completion: { (image) -> (Void) in
navBarCell.avatarImageView.image = image
})
}
But get the error Cannot convert value of type '[String]' to expected argument type 'String' on the if let line.
I've also tried removing the ! and I get the error '[Any]?' is not convertible to '[String]'; did you mean to use 'as!' to force downcast? with the fix recommendation of putting the ! back in: Replace 'as' with 'as!'
EDIT 2:
if let avatarImageUrlString = self.chatVC?.avatarDictionary.allValues as? [String], let imageUrl = URL(string: avatarImageUrlString[0]) {
ImageService.getImage(url: imageUrl, completion: { (image) -> (Void) in
navBarCell.avatarImageView.image = image
})
}
EDIT 3:
let avatars = self.chatVC?.avatarDictionary.allValues[indexPath.row] as? [String]
if let avatarImageUrlString = avatars, let imageUrl = URL(string: avatarImageUrlString[0]) {
ImageService.getImage(url: imageUrl, completion: { (image) -> (Void) in
navBarCell.avatarImageView.image = image
})
}
You're actually force-unwrapping allValues as [String] so then there is nothing to downcast. Also change type from array of strings to single string
So instead of this in optional-binding
as! [String]
use this
as? String
So...
if let avatarImageUrlString = self.chatVC?.avatarDictionary.allValues[indexPath.row] as? String, let imageUrl = URL(string: avatarImageUrlString) {
...
}
I'm developing share extension which is used on safari.
I could get url on share extension. but I cant get page title.
let puclicURL = String(kUTTypeURL)
if itemProvider.hasItemConformingToTypeIdentifier(puclicURL) {
itemProvider.loadItem(forTypeIdentifier: puclicURL, options: nil, completionHandler: {
(item, error) in
if let url: NSURL = item as? NSURL {
print("url", url)
// I want page title also
}
}
}
And, I tried below code.https://stackoverflow.com/a/33139355/5060282
I think below code can run only in Action Extension. not Share Extension.
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
let dictionary = item as! NSDictionary
OperationQueue.main.addOperation {
let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! NSDictionary
let title = NSURL(string: (results["title"] as! String))
//yay, you got the title now
print(title)
}
})
} else {
print("error")
}
// But, error...
I currently did for single image which is working fine. But i want to pick multiple images and display in app.Can any one please help me in this. Thank you.
override func didSelectPost() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
let contentType = kUTTypeImage as String
if let contents = content.attachments as? [NSItemProvider] {
for attachment in contents {
if attachment.hasItemConformingToTypeIdentifier(contentType) {
attachment.loadItemForTypeIdentifier(contentType, options: nil) { data, error in
let url = data as! NSURL
if let imageData = NSData(contentsOfURL: url) {
self.saveImage(imageData)
}
}
}
}
}
}
// Unblock the UI.
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}
Saves an image to user defaults
func saveImage(imageData: NSData) {
if let prefs = NSUserDefaults(suiteName: suiteName) {
prefs.setObject(imageData, forKey: keyName)
}
}
Reading in app
if let prefs = NSUserDefaults(suiteName: suiteName) {
if let imageData = prefs.objectForKey(keyName) as? NSData {
drawCard(imageData)
}
}
And i modified this for array, but its not working.
override func didSelectPost() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
let contentType = kUTTypeImage as String
if let contents = content.attachments as? [NSItemProvider] {
for attachment in contents {
if attachment.hasItemConformingToTypeIdentifier(contentType) {
attachment.loadItemForTypeIdentifier(contentType, options: nil) { data, error in
let url = data as! NSURL
if let imageData = NSData(contentsOfURL: url) {
galleryBucket[url] = imageData
self.saveImage(galleryBucket)
}
}
}
}
}
}
// Unblock the UI.
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}
Saves an image to user defaults.
func saveImage(galleryBucket: [NSURL:NSData]) {
if let prefs = NSUserDefaults(suiteName: suiteName) {
prefs.setObject(galleryBucket, forKey: keyName)
}
}
Here I am getting nil for galleryBucket
if let prefs = NSUserDefaults(suiteName: suiteName) {
if let galleryBucket = prefs.objectForKey(keyName) as? [NSURL:NSData] {
for imageData in galleryBucket{
drawCard(imageData.1)
}
}
}
UserDefaults doesn't seem like the best place for saving images..
Take a look at this:
Save An Image To Application Documents Folder From UIView On IOS
And this is how you can search for a specific file extenstion.
Getting list of files in documents folder
And once you have a name, this is how you retrieve it.
Get image from documents directory swift
Once my share extension is loaded, not everytime and not everything is visible for user immediately. The most common is that there you can see image, number of images, and content text. However there are cases where there is a lot more informations.
How to get access to them?
I know that within SLComposeServiceViewController there is extensionContext and its inputItems property.
Ok, so I stopped the debugger at time, and print out on console some things with following command:
po (extensionContext!.inputItems[0] as! NSExtensionItem).userInfo![NSExtensionItemAttachmentsKey]
Is it correct way to do this?
Is there usually one input item?
there was two NSItemProvider objects as attachments to first NSExtensionItem
Ok, then I print out the first of attachments:
How to get that image from that NSItemProvider and url from the next one? Can you deliver some code?
I suppose we will use
loadItemForTypeIdentifier(_:options:completionHandler:)
but do not know how.
import MobileCoreServices
There is a simple function you can apply to your code:
private func fetchAndSetContentFromContext() {
guard let extensionItems = extensionContext?.inputItems as? [NSExtensionItem] else {
return
}
for extensionItem in extensionItems {
if let itemProviders = extensionItem.attachments as? [NSItemProvider] {
for itemProvider in itemProviders {
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeText as String) {
itemProvider.loadItemForTypeIdentifier(kUTTypeText as String, options: nil, completionHandler: { text, error in
})
}
}
}
}
}
So now you know to use the loadItemForTypeIdentifier(_:options:completionHandler:) method to load your aspired data.
In your snapshots you wan to get image and url objects.
Let's begin.
guard
let items = extensionContext?.inputItems as? [NSExtensionItem],
let item = items.first,
let attachments = item.attachments
else { return }
var image: UIImage?
var url: URL?
let semaphore = DispatchSemaphore(value: 2)
for attachment in attachments {
if attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
attachment.loadItem(forTypeIdentifier: kUTTypeImage as String) { item, _ in
image = item as? UIImage
semaphore.signal()
}
}
if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
attachment.loadItem(forTypeIdentifier: kUTTypeURL as String) { item, _ in
url = item as? URL
semaphore.signal()
}
}
}
_ = semaphore.wait(timeout: .now() + 1.0)
print(String(describing: image))
print(String(describing: url))