سؤال

لدي معيار UITableViewCell حيث أستخدم خصائص النص والصورة لعرض favicon.ico وتسمية. بالنسبة للجزء الأكبر، هذا يعمل بشكل جيد حقا UIImage يدعم تنسيق ICO. ومع ذلك، بعض المواقع (مثل Amazon.com. قل) favicon.icoيمكنك استخدام قدرة تنسيق ICO على تخزين أحجام متعددة في نفس الملف. تقوم أمازون بتخزين أربعة أحجام مختلفة، على طول الطريق حتى 48 × 48.

ينتج عن هذا معظم الصور 16x16 باستثناء عدد قليل من الوقت في 32x32 أو 48x48 وجعل كل شيء يبدو فظيعا. لقد بحثت هنا، المنتدى الرسمي، الوثائق، وفي أماكن أخرى دون نجاح. لقد جربت كل ما يمكنني التفكير فيه في تقييد حجم الصورة. كان الشيء الوحيد الذي عمل طريقة غير موثقة، وأنا لست على وشك استخدامها. هذا هو أول تطبيقي وأول تجربتي مع الكاكاو (جاء من C #).

في حال لم أكن من الواضح فيما أبحث عنه، من الناحية المثالية، ستمركز النصيحة حول تحديد أبعاد UIImage بحيث تكون نسخة 48x48 ستوسيع نطاقها إلى 16 × 16 أو طريقة لتخبرها UIImage لاستخدام إصدار 16x16 الموجود في ملف ICO. لا أحتاج بالضرورة رمز: مجرد اقتراح لنهج سيفعلني بخير.

هل يوجد لدى احد أي اقتراحات؟ (سألت في المنتدى الرسمي كذلك لأنني غرقت أكثر من يوم في هذا بالفعل. إذا تم نشر الحل هناك، سأضعه هنا أيضا.)

هل كانت مفيدة؟

المحلول

إليك المستخدم "BCD" في المنتديات الرسمية نشر أنهي أنهي حل المشكلة (مع تعديل طفيف من قبل نفسي لجعلها ثابتة لإدراجها في مجموعة من أساليب المرافق):

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

نصائح أخرى

أنا أفعل شيئا مشابها في التطبيق الخاص بي.

    UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
    CGImageRef          imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    CGContextRef bitmap = CGBitmapContextCreate (NULL, thumbRect.size.width, thumbRect.size.height, 8, thumbRect.size.width*3, 
                                                                                 CGColorSpaceCreateDeviceRGB(), alphaInfo);

    CGContextDrawImage(bitmap, thumbRect, imageRef);

    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;
}

ثم قم بإنشاء صورة جديدة تقيد النسب. استبدال "الصورة" مع ملف ICO الذي تحصل عليه من Amazon.

CGRect sz = CGRectMake(0.0f, 0.0f, 16, 16);
UIImage *resized = resizedImage(image, sz);

لم يكن لديك ما يكفي من الكرمة حتى الآن للتعليق على الإجابات، لكن لدي نجاح جيد مع رمز Bbransons أعلاه.

لقد أحصل على عدد عادل من التحذيرات على وحدة التحكم على الرغم من أن إعداد البايتات لكل صف لا يكون مرتفعا بما يكفي - بعد قراءة الإجابات على هذا بريد انتهى بي الأمر بإعداده إلى 0، مما يتيح للنظام تحديد القيمة الصحيحة.

على سبيل المثال:

CGContextRef bitmap = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, CGColorSpaceCreateDeviceRGB(), alphaInfo);

إليك كيف فعلت ذلك. تعتني هذه التقنية بتحريك تسميات نص النص والتفصيل بشكل مناسب إلى اليسار:

@interface SizableImageCell : UITableViewCell {}
@end
@implementation SizableImageCell
- (void)layoutSubviews {
    [super layoutSubviews];

    float desiredWidth = 80;
    float w=self.imageView.frame.size.width;
    if (w>desiredWidth) {
        float widthSub = w - desiredWidth;
        self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,desiredWidth,self.imageView.frame.size.height);
        self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x-widthSub,self.textLabel.frame.origin.y,self.textLabel.frame.size.width+widthSub,self.textLabel.frame.size.height);
        self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x-widthSub,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width+widthSub,self.detailTextLabel.frame.size.height);
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
}
@end

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[SizableImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    cell.textLabel.text = ...
    cell.detailTextLabel.text = ...
    cell.imageView.image = ...
    return cell;
}

لا أعرف أي شيء عن كيفية عمل ملفات ICO، لذلك ربما يكون من الممكن استخراج صورة 16x16 واستخدام ذلك، لكن ليس لدي أي فكرة عن الطريقة.

بقدر ما أعرف، من غير الممكن تحديد أي خصائص مفيدة على الصورة في عام UITableViewCell. وبعد أبسط طريقة (لا تزال فوضوي، خاصة إذا كنت جديدا على ذلك) لتوسيع نطاق كل شيء إلى 16x16 سيكون في الفئة الفرعية UITableViewCell, ، اعطيه UIImageView, ، دائما حجم عرض الصورة إلى 16x16، وقم بتعيين quantsmode إلى UIViewContentModeAspectFit.

لقد عدلت فئة الأشخاص الآخرين، من أجل جعل جميع الصور تشغل نفس العرض (عرض مرئي)، دون تحجيم:

-(UIImage*) centerImage:(UIImage *)inImage inRect:(CGRect) thumbRect
{

    CGSize size= thumbRect.size;
    UIGraphicsBeginImageContext(size);  
    //calculation
    [inImage drawInRect:CGRectMake((size.width-inImage.size.width)/2, (size.height-inImage.size.height)/2, inImage.size.width, inImage.size.height)];
    UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();        
    // pop the context
    UIGraphicsEndImageContext();
    return newThumbnail;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top