I am creating an app in which i draw something over imageview using a sublayer function, now i want to create a copy of that image view (over which there is everything like sublayers and image) so that my original imageview don't get effected while accessing imageview for some other purposes. Is it possible to create a copy of image view containing all the contents and use that imageview again and again for different purposes.
Thanks
I'm not sure if you're trying to copy only the contained UIImage, or an image of all the layers. Examples for both:
import UIKit
// Original UIImageView
var imageView = UIImageView()
// Copying image into new UIImageView
var _imageView = UIImageView(image: imageView.image)
// Rendering everything inside the UIImageView into a new UIImageView
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, _imageView.opaque, 0.0);
_imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
let allLayersImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var __imageView = UIImageView(image: allLayersImage)
Related
self.backgroundColor = UIColor(patternImage: UIImage(named: "na72.png")!)
(UILABEL)
How to fit background image of label to the frame size of a label?
I should use 'self.backgroundColor = UIColor(~~)'
Because you are creating a UIColor using a patternimage, it is designed to create a tile pattern.
To work around this, you can resize your image to match the size of the frame of your label manually, or by using a function (like this one), then use your resized image as your patternimage.
Example using the above function:
let image = imageWithImage(image: UIImage(named: "na72.png")!, scaledToSize: label.frame.size)
label.backgroundColor = UIColor(patternImage: image)
This issue was also discussed here
I was wondering what I would need if I wanted to use a mask image to get my UIImageView in a specific shape. From what I understand, to create a mask, I need to have an image with the shape of the mask all black on top of a white background. Something like this, for example:
First of all, is this sufficient to shape an image view, and if so, how do I do it in Swift 3? I can only find masking code that is either outdated or written in Objective-C. I've tried simply assigning the image above to an UIImageView and then assign the image view to the mask property of the UIImageView I want to shape, like so:
self.defaultImageView.mask = self.maskImageView
This didn't do anything. It just made self.maskImageView disappear (both image view's added through the storyboard and connected using IBOutlet properties). I'm sure I'm forgetting to do something. It can't be this simple. I would appreciate it if someone could help me out. Like I said, I put both image views on the exact same spot, on top of each other, in the storyboard.
UPDATE:
My first attempt to set the mask programmatically after deleting it from my storyboard.
let layer:CALayer = CALayer()
let mask:UIImage = UIImage(named: "Black-Star-Photographic-Agency")!
layer.contents = mask
layer.frame = CGRect(x: 0, y: 0, width: ((self.defaultImageView.image?.size.width)!), height: (self.defaultImageView.image?.size.height)!)
self.defaultImageView.layer.mask = layer
self.defaultImageView.layer.masksToBounds = true
The result was that the image view had completely disappeared and wasn't visible anymore. Am I doing something, am I forgetting something or both?
You should use a png image, which supports transparency, unlike jpg.
In Photoshop your image should look similar to this:
It doesn't matter if your shape is black or white. What matters is transparency of each pixel. Opaque area (black in this case) will be visible and transparent area will get trimmed.
Edit:
You should not create mask view from storyboard if you do so. It is not going to be a part of your view hierarchy. Just add it programmatically like this:
let maskView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
maskView.image = UIImage(named: "mask")
imageView.mask = maskView
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
maskView.frame = imageView.bounds
}
Output:
Here is a test project to show how it's working.
Also if you're using a custom frame/image and run into the mask not showing properly, try setting the content mode of the mask:
maskView.contentMode = .scaleAspectFit
I'm working on a Swift iOS app where I want the user to be able to place an image (a sticker such as a beard or an eyepatch) over another image.
Currently I have it working to where they can take a photo or pull an image up from their Photos and have that display in a UIImage. Obviously next is adding the sticker. I've been able to dig up zero data on this from Google or Stack. And I mean zero.
Does anyone have a general idea of how I would execute this? Specifics would help too of course.
Here's the easy way.
have a UIView as a parent, let's say stickerView
add image to stickerView
add stickers to stickerView
save a snapshot of stickerView
Here's the code to snapshot UIView,
extension UIImage {
class func imageWithView(view: UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
Usage
let snapshotImage = UIImage.imageWithView(stickerView)
I would suggest you to add UIImageView for sticker instead of editing image when adding sticker. And after that when save the image then you should edit the image.
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)
}
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)