البرمجة

كيفية الحصول على حجم الصورة المعروضة في iOS

To get the displayed image size in pixels in iOS, you can calculate it based on the image’s scale factor and the size it’s being displayed at. Here’s how you can do it:

swift
extension UIImage { var pixelSize: CGSize { let scale = UIScreen.main.scale return CGSize(width: size.width * scale, height: size.height * scale) } } // Assuming imageView is your UIImageView instance let displayedImageSize = imageView.image?.pixelSize ?? .zero print("Displayed image size in pixels: \(displayedImageSize)")

This code extends UIImage to add a pixelSize property, which calculates the image’s size in pixels based on the device’s screen scale. Then, you can use this property to get the displayed image size in pixels.

If you want to set the size of the image view in pixels, you can do it like this:

swift
// Assuming imageView is your UIImageView instance let pixelSize = CGSize(width: 300 * UIScreen.main.scale, height: 300 * UIScreen.main.scale) imageView.frame = CGRect(origin: imageView.frame.origin, size: pixelSize)

This sets the size of the image view to 300×300 pixels on a Retina display (where the scale factor is 2). Adjust the scale factor (UIScreen.main.scale) accordingly if you’re targeting different screen resolutions.

المزيد من المعلومات

لفهم كيفية الحصول على حجم الصورة التي يتم عرضها بالبكسل في iOS، يجب أولاً فهم كيفية عمل الصور بالنسبة للأجهزة ذات الشاشات عالية الكثافة مثل iPhone Retina display.

في iOS، الصور تعرض بالـ points، ليس بالبكسل مباشرة. هذا يعني أن الصورة بحجم 100×100 points ستعرض بحجم 100×100 نقطة، ولكن على شاشة Retina display ستكون البكسلات الفعلية 200×200 بكسل.

لحساب عدد البكسلات الفعلي للصورة التي يتم عرضها، يمكنك استخدام الخاصية scale لـ UIImage و UIScreen.main.scale لمعرفة معدل الشاشة:

swift
extension UIImage { var displayedPixelSize: CGSize { let scale = UIScreen.main.scale return CGSize(width: size.width * scale, height: size.height * scale) } } // Assuming imageView is your UIImageView instance let displayedImageSize = imageView.image?.displayedPixelSize ?? .zero print("Displayed image size in pixels: \(displayedImageSize)")

هذا يعطيك حجم الصورة التي تم عرضها بالبكسل على الشاشة.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر