Animated Gif inside UITextView - ios

I had a look at this question and it didn't work. I am also trying to avoid this sort of solution. In the end I am willing to go with a CADisplayLink and change each frame manually. Still, is there a simpler way to do this?

An animated gif if just a collection of images which are displayed in sequence at a timed interval. You can do this with a UIImageView like this:
imageView.animationImages = [UIImage(named: "uploadFrame0")!, UIImage(named: "uploadFrame1")!, UIImage(named: "uploadFrame2")!, UIImage(named: "uploadFrame3")!, UIImage(named: "uploadFrame4")!, UIImage(named: "uploadFrame5")!, UIImage(named: "uploadFrame6")!, UIImage(named: "uploadFrame7")!, UIImage(named: "uploadFrame8")!]
imageView.animationDuration = 1
imageView.startAnimating()
Then you can use exclusion paths to make the text flow around it.

Use exclusion paths to define areas that are kept free, than add the image with an image view.
NSMutableArray *exclutionPaths = [NSMutableArray array];
[exclutionPaths addObject:[UIBezierPath bezierPathWithRect:CGRectInset(imageFrame, -4, 0)]];
[_textView.textContainer setExclusionPaths:exclutionPaths];
[_textView addSubview:imageView];

Related

Why would set view.layer.contents instead of using an UIImage

Recently I encountered this code:-
self.view.layer.contents = (id)[UIImage imageNamed:#"img_bg.png"].CGImage;
This setup a background for the whole view of a UIViewController.
Usually, I would just set an UIImageView as subview, taking the whole area of the view from the UIViewController
Why would someone use one technique rather than an other ?
I found this one a bit disturbing because the background does not show in the storyboard; where I expect it.
that's a great question. Deeply, you are asking the different between View and Layer, you know we can think view as layer's delegate which can handle user's interaction with app or system. when there is no any interaction but render view, we can just use layer, that will take up less memory compared with the view。
Try this
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"img_bg.png"))
Hope this will help
Cheers
You can try this:-
UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named: "img_bg.png")?.drawInRect(self.view.bounds)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.view.backgroundColor = UIColor(patternImage: image)
Hope this helps,
Thanks

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.

How to display more copies of an object?

Objective
I need my app to display multiple instances of a UIImageView. In other words, the app should take a UIImageView and display it more times inside a view. The new UIImageViews remain identical, only their position in the view changes.
Code
To do so, I firstly declare a UIImageView:
let myImage = UIImage(named: "house")
let myImageView = UIImageView(image: myImage)
myImageView.frame = CGRectMake(view.frame.size.width/2, view.frame.size.height/2, 100, 100)
I then use a for-loop to repeat the image repetition.
for (var i = 0; i != 10; i++) {
// Distribuite UIIMageViews here
self.view.addSubview(myImageView)
}
Though, I seem to find no way to copy a UIImageView and display it more times. Declaring more than one UIImageView sounds like an inefficient solution to me.
Question
Is there any way to display an object (UIImageView) multiple times by declaring it just once?
Since UIImageView doesn't conform to the NSCopying protocol, you will have to do the copy yourself: instantiate a new UIImageView and copy manually each property you think important from the original to the new one.
The following example is creating 10 new images based on the original one. They are just copying the UIImage, but you can complete it and copy the properties you need:
let myImage = UIImage(named: "house")
let myImageView = UIImageView(image: myImage)
myImageView.frame = CGRectMake(view.frame.size.width/2, view.frame.size.height/2, 100, 100)
self.view.addSubview(myImageView)
for (var i = 0; i != 10; i++) {
var newImageView = UIImageView(image: myImageView.image)
newImageView.frame = // yourNewFrame
// Copy any else properties you need from the original image
self.view.addSubview(newImageView)
}

How to do I animate a coin in swift to show that it is spinning, using multiple images?

I does not have to be real 3d,thanks. I tried to use a gif image but it did not work and just displayed the image.
You can use a UIImageView:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImageView_Class/index.html#//apple_ref/occ/instp/UIImageView/animationImages
// imageView is a UIImageView
// images is an array of UIImages
imageView.animationImages = images
imageView.animationDuration = 1.0
imageView.animationRepeatCount = 10
imageView.startAnimating()

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