UIImageView with custom border - swift

I've a problem with UIImageView with custom border.
how can i make an image like this?
I add the normal image with bread, and i have this image, the border
any idea?
thanks

All you need to do is to use a bucket fill tool to fill the star outer border.
Afterwards, just create a new UIImageView with the original imageView frame and put it on top of the original image using "bringToFront()" method:
let imgView_bread = UIImageView(image: UIImage (named: "bread"))
imgView_bread.contentMode = .ScaleAspectFill
self.view.addSubview(imgView_bread);
let imgView_starBorder = UIImageView(image: UIImage (named: "star_border"))
imgView_starBorder.contentMode = .ScaleAspectFill
imgView_starBorder.frame = imgView_bread.frame
self.view.addSubview (imgView_starBorder);
self.view.bringSubviewToFront (imgView_starBorder);

Related

image has to be circular without empty spaces in swift

I have a image downloaded from the web and i want to show the image in circular form.right now the image is not in circular form.It shows white spaces on top and bottom of the imageview.
The code used for the image is as follows:
self.profileview.layer.cornerRadius = self.profileview.frame.height / 2
self.profileview.layer.borderWidth = 1.0
self.profileview.layer.borderColor = UIColor.black.cgColor
self.profileimg.clipsToBounds = true
The image which appears is as follows:
enter image description here
How to make the image circular?
try this , may be it will work in your case .
myimageview.contentMode = .scaleAspectFill
or
myimageviewcontentMode = .scaleToFill

NSView dataWithPDF inside rect but with background view

I wan´t to take a screenshot of a NSView but when I do this, I get an image without the background. All the subviews are show, but not the background. I use this code:
if let image = NSImage(data: background.dataWithPDF(inside: background.bounds)) {
let imageView = NSImageView(image: image)
imageView.imageScaling = .scaleProportionallyUpOrDown
return NSImageView(image: image)
}
I thought, ok when I get only the subviews, then I will make a screenshot of the superview and I thried the following:
if let superview = background.superview, let image = NSImage(data: superview.dataWithPDF(inside: background.frame)) {
let imageView = NSImageView(image: image)
imageView.imageScaling = .scaleProportionallyUpOrDown
return NSImageView(image: image)
}
But I get the same result. Even if I set the background color of my background view I don´t get an image without transparent background.
How can I resolve this?
thank you
Artur
I got an Answer:
background.lockFocus()
if let rep = NSBitmapImageRep(focusedViewRect: background.bounds){
let img = NSImage(size: background.bounds.size)
img.addRepresentation(rep)
return NSImageView(image: img)
}
background.unlockFocus()
You could have converted the views backing layer.backgroundColor to an image. Then assign the image to a backing image view that sits in the 0th index of your views heirarchy and use the method dataWithPDF -> that will successfully capture a background.

uibezierpath with multiple line width and a background image (swift)

I'm trying to draw several shapes (rectangle, triangle, circle, ...) in swift and for that, I use uibezierpath but I am not able to draw exactly what I want.
I need to draw for example a rectangle, but the borders of this rectangle need to have different line width.
To do that, I create different path then use the "appendpath" to merge them in one path. So that works, BUT I also need to have a background image in this rectangle.
For that, I create a layer and set it an image. The issue is that, no background image are displayed when I use "appendpath", certainly because it doesn't recognize my drawing as a rectangle.
I hope it is clear enough, but is there a way to draw a shape with a background image, and have different border width ?
Thanks for your help !!
There are two solutions I'd suggest you try:
1) Masking
Create a normal CALayer and set the image as its contents. Then create a CAShapeLayer with the path you like and use it as the first layer's mask.
E.g.:
let imageLayer = CALayer()
imageLayer.contents = UIImage(named: "yourImage")?.CGImage // Your image here
imageLayer.frame = ... // Define a frame
let maskPath = UIBezierPath(...) // Create your path here
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.CGPath
imageLayer.mask = maskLayer
Don't forget to set the right frames and paths, and you should be able to achieve the effect you wanted.
2) Fill color
Create a CAShapeLayer with the path you like, then use your image as its fillColor.
E.g.:
let path = UIBezierPath(...) // Create your path here
let layer = CAShapeLayer()
layer.path = path.CGPath
let image = UIImage(named: "yourImage") // Your image here
layer.fillColor = UIColor(patternImage: image!).CGColor
You may find this approach easier at first, but controlling the way the image fills your shape is not trivial at all.
I hope this will help.
If you'd like more details, please provide an image or a sketch of what you're trying to achieve and / or the code you've written so far. Thanks!

Colorize image, take the wrong color swift

I want to colorize an image from an imageView. After that I want to set this image to a button.
let imageView:UIImageView = UIImageView()
if let myImage = UIImage(named: "ic_star") {
let tintableImage = myImage.imageWithRenderingMode(.AlwaysTemplate)
imageView.image = tintableImage
}
imageView.tintColor = UIColor.redColor()
bestListButton.setImage(imageView.image, forState: .Normal)
This is a snippet, that should work, but it colorize my button-image always in blue.
The debugger say that the color doesn't change. And the simulator change it in blue.
It seems that it doesn't have something to do with my backgroundcolor of image.
EDIT and (hack) solution
Okay, it's quite strange. The blue is because in my interface builder was the tintColor blue.
I changed it to white. Now my star is white.
BUT: When I delete my code, the image switch back to black. So the change of color seems just work in combination with interface builder and code.
EDIT 2
Programmatically solution:
bestListButton.tintColor = UIColor.redColor()
Is your image a PNG file?
Try:
let imageView:UIImageView = UIImageView()
if let myImage = UIImage(named: "ic_star") {
imageView.image = imageView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
}
imageView.tintColor = UIColor.redColor()
bestListButton.setImage(imageView.image, forState: .Normal)

SKSpriteNode (with texture) with wrong orientation

I have a SpriteKit project with which I'm using Swift to take an image or chose an image and display it. I know how to take or chose the image, but the problem is when I'm displaying it. Whenever I was displaying it, it would have the wrong orientation. So I used this code to orientate it:
let orientedImage = UIImage(CGImage: chosenImage.CGImage!, scale: 1, orientation: chosenImage.imageOrientation)
This worked very well on a UIImageView, but didn't work on a SKSpriteNode. This is the code I have for displaying the image on a UIImageView and on a SKSpriteNode:
let imageview = UIImageView(image: orientedImage) // Displaying the image on a UIImageView
self.view!.addSubview(imageview)
let texture = SKTexture(image: orientedImage)
let node = SKSpriteNode(texture: texture) // Displaying the image on a SKSpriteNode
self.addChild(node)
I have no idea why this doesn't work on a SKSpriteNode, but works on a UIImageView. Please help... Thanks in advance :)