أي تقنيات مضادة للتعليم سريعة وقذرة ل uiimageView المتناوب؟
-
16-09-2019 - |
سؤال
لدي uiimageview (كامل الإطار والمستطيل) الذي أدويره مع CgaffinTransform. uiimage of the uiimageview يملأ الإطار بأكمله. عندما يتم تدوير الصورة ورسمت الحواف تبدو مشاجرة بشكل ملحوظ. هل هناك أي شيء يمكنني القيام به لجعله يبدو أفضل؟ من الواضح أنه لا يجري مكافحة التعيسي مع الخلفية.
المحلول
تذكر تعيين خيارات مكافحة الاسم المستعار المناسبة:
CGContextSetAllowsAntialiasing(theContext, true);
CGContextSetShouldAntialias(theContext, true);
نصائح أخرى
حواف طبقات CoreAnimation ليست فضفاضة بشكل افتراضي على نظام التشغيل iOS. ومع ذلك، هناك مفتاح يمكنك تعيينه في Info.Plist التي تمكن من الحواف من الحواف: Uiviewedgeantialiasing.
إذا كنت لا تريد أن يكون للأداء العلاجية لتمكين هذا الخيار، فإن العمل هو أضف الحدود الشفافة 1PX حول حافة الصورة. وبعد هذا يعني أن "حواف" الصورة لم تعد على الحافة، لذلك لا تحتاج إلى علاج خاص!
API الجديدة - iOS 6/7
يعمل أيضا ل iOS 6، كما لاحظت Chris، ولكن لم يتم الإعلان عنها حتى iOS 7.
منذ IOS 7، لدى Calayer عقار جديد allowsEdgeAntialiasing
الذي يفعل بالضبط ما تريد في هذه الحالة، دون تكبد النفقات العامة لتمكينه على جميع وجهات النظر في طلبك! هذه هي خاصية Calayer، لتمكين هذا من أجل استخدام Uiview myView.layer.allowsEdgeAntialiasing = YES
.
فقط أضف الحدود الشفافة 1PX لصورتك
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
[image drawInRect:CGRectMake(1,1,image.size.width-2,image.size.height-2)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
ما عليك سوى إضافة "يجعل" مع حافة الحافة "مع نعم في المعطف وسيعمل.
أود أن أوصي تماما المكتبة التالية.
http://vocaro.com/trevor/blog/2009/10/12/Resize-a-uiimage-the-right-way/
أنه يحتوي على الكثير من الامتدادات المفيدة ل UIImage التي تحل هذه المشكلة وتشمل أيضا التعليمات البرمجية لتوليد الصور المصغرة وما إلى ذلك.
استمتع!
أفضل طريقة وجدت أن لديها حواف ناعمة وصورة حادة هي القيام بذلك:
CGRect imageRect = CGRectMake(0, 0, self.photo.image.size.width, self.photo.image.size.height);
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
[self.photo.image drawInRect:CGRectMake(1, 1, self.photo.image.size.width - 2, self.photo.image.size.height - 2)];
self.photo.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
إضافة المعلومات "Applist.Plist" مثل بعض الأشخاص يصفون لها ضربة كبيرة على الأداء وإذا كنت تستخدم ذلك، فأنت كذلك في تطبيقه بشكل أساسي على كل شيء بدلا من المكان الوحيد الذي تحتاجه فقط.
أيضا، لا تستخدم فقط UIGraphicsBeginImageContext(imageRect.size);
وإلا فإن الطبقة ستكون ضبابية. يجب عليك استخدام UIGraphicsBeginImageContextWithOptions
كما أظهرت.
لقد وجدت هذا الحل من هنا, ، وهذا مثالي:
+ (UIImage *)renderImageFromView:(UIView *)view withRect:(CGRect)frame transparentInsets:(UIEdgeInsets)insets {
CGSize imageSizeWithBorder = CGSizeMake(frame.size.width + insets.left + insets.right, frame.size.height + insets.top + insets.bottom);
// Create a new context of the desired size to render the image
UIGraphicsBeginImageContextWithOptions(imageSizeWithBorder, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Clip the context to the portion of the view we will draw
CGContextClipToRect(context, (CGRect){{insets.left, insets.top}, frame.size});
// Translate it, to the desired position
CGContextTranslateCTM(context, -frame.origin.x + insets.left, -frame.origin.y + insets.top);
// Render the view as image
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Fetch the image
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
// Cleanup
UIGraphicsEndImageContext();
return renderedImage;
}
الاستعمال:
UIImage *image = [UIImage renderImageFromView:view withRect:view.bounds transparentInsets:UIEdgeInsetsZero];