Fit the image to the screen - iphone

I have the same image in all size(320x480 640x960 640x1136 750x1334 1242x2208) and I use these image for Launch Screen ( Assets -> New iOS Launch Image).
I would like to use these image(in UIImageView) in run time of the app selecting the image proper for size screen the device in use.
In Launch Screen is perfect, but if I use the LaunchImage for UIImageView, the image in view is different.
In this link there is my test code(Launch.zip) https://github.com/Volpesio/Launch.git
This code run the Launch Image(perfectly) and after it displays one UIImageView, it should use the same image of Launch but is didifferent
How can I resize or how can I select the right image for the the device in use?
You can help me, please..

I solved it with this code:
extension UIImage {
convenience init?(fullscreenNamed name: String) {
switch UIScreen.mainScreen().bounds.size.height {
case 480: //iPhone 4/4s
self.init(named: "\(name)-700#2x.png")
case 568: //iPhone 5/5s
self.init(named: "\(name)-700-568h#2x.png")
case 667: //iPhone 6/6s
self.init(named: "\(name)-800-667h#2x.png")
case 736: //iPhone 6+/6s+
self.init(named: "\(name)-800-Portrait-736h#3x.png")
default:
self.init(named: name)
}
}
}
I take in "LaunchImage" the right image for the device screen in use

Related

How to implement SwiftUI’s .onDrag modifier with NSImage (macOS)

I am building a sandboxed app for macOS with SwiftUI. I have a NSImage that is displayed as Image(nsImage: myNSImage). I want that View to support drag and drop, meaning that it can be dragged to any location that can receive image files.
Here’s my approach:
Image(nsImage: myNSImage)
.onDrag({
let itemProvider = NSItemProvider()
itemProvider.suggestedName = "image.png"
itemProvider.registerDataRepresentation(for: UTType.png) {
loadHandler in
loadHandler(nsImage.pngRepresentation, nil)
print("loadHandler completed") // Never prints !!
return nil
}
return itemProvider
})
This way I can drag the View. But it seems like it isn’t able to provide the image.
The “drop”, i.e. on the Desktop, is simply not working. I would expect that it saves an "image.png" in the destination URL, but it doesn’t.
How can I implement .onDrag so that it provides a image file based on NSImage?
Edit: I already have tried different UTTypes, for example .tiff in combination with nsImage.tiffRepresentation without luck.

Statusbar.image has wrong color

I am using following code for my status bar image:
let icon = NSImage(imageLiteralResourceName:"flag")
statusBarItem.image = icon
This leads to wrong color for certain background colors / modes. In the picture, what's white should be black. The image resource is white/transparent. If I change that, I get the same problem. All other status bar images will turn white on certain configurations, mine will stay black.
I was thinking that MacOS would add effects to make all statusbar icons look uniform on it's own, but apparently thats not the case...
Any ideas how to fix that?
Thanks!
MacOs can do what you want. I recommend reading Apple documentation:
https://developer.apple.com/documentation/uikit/appearance_customization/supporting_dark_mode_in_your_interface
Basically you have 2 options if you don‘t provide the code manually.
Option 1. In Xcode, navigate to your image asset in assets.xcassets. In the attributes pane, in „Render as…“ specify „Template Image“. This worked well for my menu bar app.
Option 2. Supply different versions of your icon in one image asset, macOs will then choose the appropriate version.
I found a solution. Again I realize that MacOS development is way less supported by Apple than iOS. I think the color adjustment of statusbar icons should be the task of the operating system, but Apple lets the developer do the work. Whatever.
Here is the solution:
You have to provide two versions of your icon, one in black, the other in white.
When the app launches, you have to check wether the user's MacOs is in dark or light mode. This can be done with following code:
let mode = UserDefaults.standard.string(forKey: "AppleInterfaceStyle")
if (mode == "Dark"){
let icon = NSImage(imageLiteralResourceName:"flag")
statusBarItem.image = icon
} else {
let icon = NSImage(imageLiteralResourceName:"flagDark")
statusBarItem.image = icon
}
One problem remains here now: When the user changes the mode while your app is running, the icon color won't update. Also: If the user uses the automatic mode (i.e. it's light at day and dark in the night), the icon color won't switch as well.
You can tackle that problem by listening to a certain notification that is fired when the dark mode settings changes:
DistributedNotificationCenter.default.addObserver(self, selector: #selector(updateIcon), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
#objc func updateIcon(){
print("updateIcon ausgeführt")
let mode = UserDefaults.standard.string(forKey: "AppleInterfaceStyle")
if (mode == "Dark"){
let icon = NSImage(imageLiteralResourceName:"flag")
statusBarItem.image = icon
} else {
let icon = NSImage(imageLiteralResourceName:"flagDark")
statusBarItem.image = icon
}
}
In my tests, this worked in all scenarios.
For me this worked:
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
// ...
func applicationDidFinishLaunching(_ aNotification: Notification) {
// ...
if let button = statusItem.button {
let image = NSImage(named: NSImage.Name("TrayIcon"))
image?.isTemplate = true
button.image = image
}
// ...
}
// ...
}

TraitCollectionDidChange behaves different for iphone X

In My case i am changing my layouts of the view depending on the traitCollection.horizontalSizeClass
Here is my Code snippet .
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.horizontalSizeClass == .regular {
// 2
NSLayoutConstraint.deactivate(compactConstraints)
NSLayoutConstraint.activate(regularConstraints)
// 3
socialMediaView.axis = .horizontal
} else {
// 4
NSLayoutConstraint.deactivate(regularConstraints)
NSLayoutConstraint.activate(compactConstraints)
socialMediaView.axis = .vertical
}
}
Every things working as directed in iphone 7 plus and iphone x in portrait-mode
and in landscape mode i want the rabbit image comes to left side and the stackview of all socialMedias axis will be horizontal
but in iphone X landscape mode its not coming while in phone 7 its coming .Check the below screen shots
Looking at your question and condition, I found a problem in your condition. you should check for the verticalSizeClass instead of horizontalSizeClass.
WHEN CHECK FOR HORIZONTAL SIZE CLASS.
IN PORTRAIT: All iPhone Devices has compact width, so every time, it will go to else condition and set the view properly.
IN LANDSCAPE: All iPhone Plus (iPhone 6s Plus, iPhone 7 Plus and iPhone 8 plus) Devices has Regular width and All other iPhone (iPhone 6s, 6SE, iPhone 7 and iPhone 8, iPhone X) Devices has Compact width, so for all plus devices it will works fine but not for others.
FOR PORTRAIT:
FOR LANDSCAPE:
For more, read official doc here
So, update your code to this.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.verticalSizeClass == .regular {
NSLayoutConstraint.deactivate(compactConstraints)
NSLayoutConstraint.activate(regularConstraints)
socialMediaView.axis = .horizontal
} else {
NSLayoutConstraint.deactivate(regularConstraints)
NSLayoutConstraint.activate(compactConstraints)
socialMediaView.axis = .vertical
}
}
Try and share the results.

Capture dual pictures with iPhone 7Plus and AVCaptureSession

I am working on this piece of swift 3 code:
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
let videoCaptureDevice = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera,mediaType: AVMediaTypeVideo, position: .back)
let videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
if captureSession.canAddInput(videoInput)
{
captureSession.addInput(videoInput)
}
Then, i take a picture with an AVCapturePhotoOutput object and i get the picture in AVCapturePhotoCaptureDelegate object.
It works fine.
What i want to do is to take a picture with the iPhone 7 Plus dual camera. I want to get 2 pictures, like the official iOS camera app:
- One picture with background blur
- A second picture, without blur
Do you think it is possible ?
Thanks

How to let iPhone 6 use Retina 4 2x image size in Xcode 6

I'm using swift for my game, and i need to know how to let iPhone 6 use Retina 4 2x in Xcode.
My images are green-square#2x green-square-568h#2x green-square#3x
My objectif : is to force iPhone 6 use Retina 4 2x image for sprite
i use this code to apply image for my sprite
let GSquare = SKSpriteNode(imageNamed: "green-square")
and How i can load specific image from assets like
let GSquare = SKSpriteNode(imageNamed: "green-square-568h#2x")
Thanks
You are not forced to use the assets catalog that has device-type sized images 1x, 2x, 3x....
You can include PNG or JPG images of any resolution in your app resources directly, and then load the image by its full file name, to ensure that you use a specific image resolution sized exactly as you want that way. When you load a specific file, you have to specify the extension in the filename for the UIImage().
Instead of:
var image = UIImage(named: "myGraphic") //load from assets
Use this form:
var image = UIImage(named: "myGraphic.png") //load from file
Keep in mind that for UIImageView, the image is scaled to the size of your UIImageView subview. That means your image will be stretched or shrink if the rectangle you create by the view's frame or Auto Layout constraints is not the exact size of the image you loaded.
You can detect which device you're on like this in your view controller:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
adjustViewLayout(size)
}
func adjustViewLayout(size: CGSize) {
switch(size.width, size.height) {
case (320, 480): // iPhone 4S in portrait
// do something here
case (480, 320): // iPhone 4S in landscape
// do something here
case (320, 568): // iPhone 5/5S in portrait
// do something here
case (568, 320): // iPhone 5/5S in landscape
// do something here
case (375, 667): // iPhone 6 in portrait
// do something here
case (667, 375): // iPhone 6 in landscape
// do something here
case (414, 736): // iPhone 6 Plus in portrait
// do something here
case (736, 414): // iphone 6 Plus in landscape
// do something here
default:
break
}
view.setNeedsLayout()
}
Also Put this in your viewDidLoad() method so that it is detected the first time the view loads as the other method only detects orientation changes:
adjustViewLayout(UIScreen.mainScreen().bounds.size)