I have a working code where I copy an image to UIPasteboard, but I cannot find a way to implement the PASTE functionality programmatically. Any idea or tip?
The following piece of code might work. Make sure you are testing on the device.
let image = UIImage(named: "person.png")
UIPasteboard.generalPasteboard().image = image;
based on the comment, you can do it as follows. I am putting here the objective-c code, I hope you could get the idea then convert it to the swift.
NSData* pasteData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString*)kUTTypeJPEG];
you may find the swift solution in the following url
Swift UIPasteboard not copying PNG
Swift 3
If an imaged has been copied from another application (e.g. Safari) this is how I add it into my app. I trigger this from a UIAlertController as a UIAlertAction.
let pasteboard = UIPasteboard.general
if pasteboard.hasImages {
myImage.image = pasteboard.image
}
Alternatively, you may use NSData. It works on Simulator too.
// Copy
let image = UIImage(named: "sample")
let imageData = UIImageJPEGRepresentation(image!, 1)
UIPasteboard.general.setData(imageData!, forPasteboardType: "image")
// Paste
let imageData = UIPasteboard.general.data(forPasteboardType: "image")
let image = UIImage(data:imageData!)
copiedImage.image = image
Related
I have a document's directory path of a gif I'd like to post to Facebook, but unfortunately when I use this process, it posts a static image instead of it being animated. I know it's animated because I can save the same image to my camera roll and see that it animates. Is there anyway of posting an animated version of this GIF?
let image = UIImage(contentsOfFile: path) // document directory path
let sharePhoto = FBSDKSharePhoto()
sharePhoto.image = image
let content = FBSDKSharePhotoContent()
content.photos = [sharePhoto]
let shareDialog: FBSDKShareDialog = FBSDKShareDialog()
shareDialog.shareContent = content
shareDialog.mode = .native
shareDialog.show()
This is not as issue with your image due to Facebook natively not support gif image directly uploaded on it.
You can share gif image link on Facebook and it will show there gif image. Like as commonly peoples are using giphy, tenor website and share gif image link from this website. So gif image display via this type of website.
I hope this will help you.
I'm sure this is a simple question, but I'm still pretty new to Swift, so I can't figure it out. Basically, I have followed this tutorial to implement a custom loading animation. It's working fine in my app with the demo image, however I can't figure out how to add a URL from a different class.
I need to be able to set the url of the image in viewDidAppear and this is also when I would like the animation to start.
Can anyone help me out?
You need to create an NSURL for the URL of the image you want to get.
Then create an instance of NSData with contentsOfUrl to get the URL as data.
Then create a UIImage with the NSData
Then update the imageView with the new image
let myPhotoUrl = NSURL(string: "http://myfancypics.com/abc.jpg")
let photoDataFromUrl = NSData(contentsOfURL: myPhotoUrl!)
let myImage = UIImage(data: photoDataFromUrl)
imageView.image = myImage
I have a UIPageViewController showing a UIViewController that contains a UIImageView.
The images displayed in the UIImageView originally come from the camera roll. I copy them in the application's folder. When I display the UIPageViewController, it loads the image for the current page and displays it:
if let image = UIImage(contentsOfFile: imagePath) {
imageView.image = image
}
The problem is that it's really slow. Loading the first image is slow and then swiping is very slow.
I am testing this on my iPhone 5. I installed the app through XCode. Is there a way to make it faster? Should I save a smaller version of the image when I save the images from the camera roll into my application's folder?
You could preload the images like Sergii suggested or load the images a background thread to avoid blocking the main thread.
NSOperationQueue().addOperationWithBlock {
if let image = UIImage(contentsOfFile: imagePath) {
NSOperationQueue.mainQueue().addOperationWithBlock {
self.imageView.image = image
}
}
}
You can load images you'll need before transitioning to UIPageViewController. Also, take a look at some projects for image caches
FastImageCache, SDWebImage, Haneke
Also, you can be interest in this article Avoiding Image Decompression Sickness
To increase the speed of swiping you'd need to move where you are assigning the image to the UIImageView from the main thread to a background thread.
There are a few ways to do this...
Using NSOperationQueue
NSOperationQueue().addOperationWithBlock {
if let image = UIImage(contentsOfFile: imagePath) {
NSOperationQueue.mainQueue().addOperationWithBlock {
self.imageView.image = image
}
}
}
Or you can use GCD and calling dispatch_async()
if let image = UIImage(contentsOfFile: imagePath) {
dispatch_async(dispatch_get_main_queue()) {
imageView.image = image
}
}
With regards to saving an image from the Camera Roll when/if you save it to your apps file system you could try using:
let qualityFloat = 1.0
let imageData = UIImageJPEGRepresentation(imageFromCameraRoll, qualityFloat)
Playing around with qualityFloat (between 0.0 - 1.0) will change the file size of the image which could help you with the loading times.
When logging in with fb, the following UIView displays the users profile picture
#IBOutlet var profilePictureView : FBProfilePictureView
How would I get the image so that I can use it elsewhere/save it to a server?
Here is the fb documentation on FBProfilePictureView (in Objective-C) https://developers.facebook.com/docs/reference/ios/current/class/FBProfilePictureView
Here is a swift port of the fb login, which works well.
https://www.snip2code.com/Snippet/68653/This-is-a-swift-port-of-the-official-Fac
And here is the same code, but in objective C
How can I convert FBProfilePictureView to an UIImage?
you can use alternate Idea also,
Objective-C
NSString *userImageURL = [NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large", [FBuser objectID]];
change the types also
type: small, normal, large, square
Swift
let avatar = "https://graph.facebook.com/\(user.objectID)/picture?width=640&height=640"
or another choice
let url = NSURL.URLWithString("https://graph.facebook.com/\(user.objectID)/picture?width=640&height=640");
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
yourimagename.image = UIImage(data: data!)
As explained in the answer you linked to (https://stackoverflow.com/a/12479572/2274694), you can treat the FBProfilePictureView as you would any UIView and traverse the subviews in order to find the UIImageView and access the image:
var image:UIImage!
for obj in profilePictureView.subviews {
if obj is UIImageView {
let objImg = obj as UIImageView
image = objImg.image
break
}
}
I am creating Custom GIF Keyboard in iOS8 using app extension. I was created layout of my custom keyboard. I was implement LongPress Gesture for selecting GIF Image but its not work. so what can i do for this or any suggestion regarding to this?
- (void)textWillChange:(id<UITextInput>)textInput {
[self.textDocumentProxy insertText:#"Hi"];
}
I also tried with UITextInputDelegate mentioned above.
This method is also deals with only text append. But My concern is to load gif or png image in input area.
I have done with long press gesture and make a method for touch handler.
-(void)tapped:(id)sender
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImage *image = [UIImage imageNamed:#"img_temp.gif"];
NSData *imgData = UIImagePNGRepresentation(image);
[pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
}
I also given become first responder aswell.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
I can copy text in pasteboard, but I can't copy .gif or .png image.
What Popkey, for example, are doing is copy the clicked GIF to the pasteboard.
The user then needs to long click their rich text field and paste the content into their iMessage for example.
You couldn't load a gif directly in the input area. To do that you have to copy the gif to the pasteboard and then paste the gif in the input area.
To copy the gif into the pasteboard you need to convert it into the NSData.
Here is the demo code in Swift 3.0.
let pb = UIPasteboard.general
let data = NSData(contentsOfURL: url)
if data != nil
{
self.pb.setData(data!, forPasteboardType: kUTTypeGIF as String)
}