Get an image from FBProfilePictureView swift - ios

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
}
}

Related

Memory issue decodeObjectForKey (Swift project)

Within an Swift application exporting the individual single viewController images (using the drawViewHierarchyInRect).
For each viewController I recover the contents of imageViews as follows:
self.imageView = (aDecoder.decodeObjectForKey("imageLevel") as? UIImageView)
self.imageView!.contentMode = UIViewContentMode.ScaleAspectFit
self.imageView!.multipleTouchEnabled = true
self.imageView!.autoresizesSubviews = true
self.addSubview(imageView!)
After 15 "page": crash. If I comment these lines, the code works again. Is there an alternative to decodeObjectForKey?
I'm not really sure what you're doing here, but you shouldn't be trying to encode a UIImageView and save it off as the view is tied to other items. You could encode the UIImage instead. Your UIImageView should come from your storyboard/xib and then you set the image you decoded to the UIImageView.

How do I paste an image programmatically with Swift?

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

Set image URL for custom image loading animation from different class

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

UIImage slow loading in a UIPageViewController

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.

Generating the image on the screen in IOS Simulator

so what I currently wish to accomplish is for an image to be printed on the background of the IOS simulator screen, so inside of my viewDidLoad, I have this
var img = UIImage(named: "paper.jpg")
This can create the image variable, but I haven't found how to display it on the screen yet. It may seem like a trivial problem, but I haven't found any documentation on this online after searching for awhile. Thanks for reading.
Refer to the UIColor documentation.
In Swift, you have to call a convenience initializer. This is because in Swift, all Objective-C class methods which return an instance of their class become convenience initializers.
Here's how it looks in Swift:
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "paper.jpg"))
+ (UIColor *)colorWithPatternImage:(UIImage *)image returns a UIColor instance, so it will become a convenience initializer in Swift. Similarly, UIImage imageNamed: becomes init(patternImage image: UIImage!).
since this is the marked answer, I felt the need to add a bit more code for completion.
as #senior has posted in his answer another way to add an image to your background is by the use of adding a UIImageView as a subview like so:
let img = UIImage(named: "paper.jpg")
let imgView = UIImageView(image: img)
self.view.addSubview(imgView)
You have to add your image to an ImageView and add this to the current view as a subview,
let img = UIImage(named: "paper.jpg")
let imgView = UIImageView(image: img)
self.view.addSubview(imgView)

Resources