Set image URL for custom image loading animation from different class - ios

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

Related

Swift iOS Facebook SDK: How to share an animated GIF?

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.

UIImageView displays downloaded images from HTTP, but won't display image resource

In my first View Controller after a user logs in, there is a quick check to see if the user has uploaded a custom image to his account. (If he has, there's a URL in the user's profile data.)
These images are downloaded correctly in my code and they show up beautifully. The problem comes when there is no custom user image, and the VC tries to set the same UIImageView to a default picture (in xcassets). The picture is being loaded into the bundle, as far as I can tell, and it's a valid PNG file.
Here is the snippet for setting the image. If a custom URL is not found, I set the URL parameter string to "nil."
-(void) setImageWithUrl: (NSString *) Url Imageview: (UIImageView *) image {
if (Url.length > 4) {
NSURL *url = [NSURL URLWithString:Url];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *Profileimage = [UIImage imageWithData:data];
[image setImage:Profileimage];
}
else {
UIImage *Profileimage = [UIImage imageNamed:#"DefaultPicture"];
[image setImage:Profileimage];
}
}
This one's driving me nuts, so all weird ideas are welcome. :-)
Let me know if you want me to post any other parts of the code that you think could be a factor.
It looks like debug-need issue. But why don't you use any image download/cache library, like SDWebImage?
It gonna be much efficient and convenient

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

Unable to add UIImageView Xcode Swift

For some reason I am unable to add a UIImageView to my app. This is the code I am using and I have searched for quite a while to figure this out but haven't had any luck.
super.viewDidLoad()
let cloudimage = UIImage(named: "cloud")
let cloudView = UIImageView(image: cloudimage)
self.view.addSubview(cloudView)
cloudView.frame = CGRectMake(0,0,100,200)
The image is a .png in my assets folder so I don't think it's that. I do have auto layout settings enabled if that is an issue? I know it can be an issue with moving a UIImageView around by using its frame, but I think I should still be able to place the image in the View no problem with this code.
I am not quite sure what to do any suggestions would be great, this is extremely frustrating.
If your image is nil, your UIImageView will not render anything. Try debug your view hierarchy.
https://developer.apple.com/library/tvos/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/special_debugging_workflows.html
http://www.raywenderlich.com/98356/view-debugging-in-xcode-6
you need to add UIimageview to main view.
self.view.addSubview(cloudView)
Try by giving the image extension also while setting to UIImage
let cloudimage = UIImage(named: "cloud.png")
let cloudView = UIImageView(image: cloudimage)
cloudView.frame = CGRectMake(0,0,100,200)
self.view.addSubview(cloudView)
Checked and its working.

Titanium Appcelerator - Remote URL not showing up as background image to View

So here's a nice one. I'm creating a imageView by doing this:
var tagView = Titanium.UI.createImageView({
backgroundImage: 'http://www.travelandtourworld.com/wp-content/uploads/2013/07/google-logo.jpg',
height:150,
width:365,
zIndex:10000
});
The problem is - anytime I use a remote URL as a background image it doesn't show up. Has anyone run into this and is there a good workaround for it?
It's just a rough guess but does it work when you use a normal View instead of an ImageView? Or try the image-property instead of backgroundImage-property for the ImageView. I just think that a background image is not the best practice to do on an ImageView even though the docs say it's possible.
I've done some testing with this as well and also found that backgroundImage doesn't work for remote URLs.
I've sort of fixed it by hacking this code into TiUtils.m of the Appcelerator core (tested with 3.5.0.GA).
if (resultImage == nil) {
if ([image isKindOfClass:[NSString class]]) {
NSURL* imageURL = [TiUtils toURL:image relativeToURL:nil];
resultImage = [[ImageLoader sharedLoader] loadRemote:imageURL];
}
}

Resources