admin管理员组文章数量:1431037
if(image != nil){
mediaSize = String(image!.size.width) + "|" + String(image!.size.height)
}
The result should be like "400|300"
I get the error: No exact matches in call to initializer
image
is UIImage
and I'm uploading it to the server before I try to convert the size to string and it's valid
Other questions about this aren't helping
if(image != nil){
mediaSize = String(image!.size.width) + "|" + String(image!.size.height)
}
The result should be like "400|300"
I get the error: No exact matches in call to initializer
image
is UIImage
and I'm uploading it to the server before I try to convert the size to string and it's valid
Other questions about this aren't helping
Share Improve this question edited Nov 19, 2024 at 12:10 David Pasztor 54.9k9 gold badges97 silver badges127 bronze badges asked Nov 19, 2024 at 11:55 TheGreatCornholioTheGreatCornholio 1,5451 gold badge23 silver badges49 bronze badges 1- Sorry, it's UIImage – TheGreatCornholio Commented Nov 19, 2024 at 11:57
3 Answers
Reset to default 1The problem is that both height
and width
are CGFloat
, which cannot be directly converted to a String
using String.init
.
You can either convert them to Double
and then initialise a String
from the Double
guard let image else { return }
let width = Double(image.size.width)
let height = Double(image.size.height)
mediaSize = String(width) + "|" + String(height)
Or simply use String interpolation, which does support CGFloat
.
mediaSize = "\(image.size.width) | \(image.size.height)"
However, be aware that you're converting floating point numbers to a String
, so you might end up with a non user friendly value with tons of decimal places. If you know that your values are integers, convert them to integers using Int(image.size.width)
or format them to a fixed number of decimal places using either String(format:_:)
or a NumberFormatter
.
You can also use String(format:...)
:
var mediaSize: String = ""
let image = UIImage(named: "myImage")
if let image {
mediaSize = String(format: "%0.0f|%0.0f", image.size.width, image.size.height)
print(mediaSize)
}
The %0.0f
means convert float to string, with NO decimals.
So your result would be "400|300"
Here’s a clean and reusable extension for converting an image size to a formatted string. This ensures your code is modular, readable, and easy to use across your project. UIImage Extension
import UIKit
extension UIImage {
/// Returns the image size as a formatted string: "width|height".
func formattedSizeString(separator: String = "|") -> String {
return "\(size.width)\(separator)\(size.height)"
}
}
How to Use It
With this extension, you can directly call formattedSizeString()
on any UIImage
instance:
if let image = image {
let mediaSize = image.formattedSizeString() // Default separator "|"
print("Media size: \(mediaSize)")
}
// Using a custom separator
if let image = image {
let mediaSize = image.formattedSizeString(separator: "x")
print("Media size: \(mediaSize)") // Example: "1920x1080"
}
Key Features
Default Separator: The default separator is "|", but you can customize it when calling the method. Code Reusability:
By encapsulating the logic in an extension, you avoid repeating string formatting logic throughout your code. Readability:
Using a named method
(formattedSizeString)
makes it clear what the function does.
本文标签: iosConvert UIImagesize to StringStack Overflow
版权声明:本文标题:ios - Convert UIImage.size to String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745563627a2663626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论