2012-10-05 12 views
6

में सीमाओं से बाहर छवि दिखाएं, मुझे एक छवि और छवि के लिए नाम दिखाने के लिए एक अनुकूलित यूआईसीओलेक्शन व्यूसेल बनाना है, और छवि के शीर्ष पर, एक फ्रेम छवि है जो 2 पिक्सेल बड़ी है छवि की तुलना में चौड़ाई और ऊंचाई। हालांकि, कोई फर्क नहीं पड़ता कि मैं क्या करता हूं, छवि फ्रेम छवि से बड़ी प्रतीत होती है। मैंने तालिका दृश्य के लिए एक ही काम किया, और यह सही काम करता है।UIImageView एक UICollectionViewCell उप वर्ग

//GridCell.h 

@interface GridCell : UICollectionViewCell 
@property(nonatomic, strong) UILabel *lblName; 
@property(nonatomic, strong) UIImageView *image; 
@end 

//GridCell.m 

#import "GridCell.h" 

@implementation GridCell 

@synthesize image, lblName; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

     UIImage *bg = [UIImage imageNamed:@"borderUIimgLg.png"]; 

     UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.width)]; 
     [bgImage setImage:bg]; 
     [bgImage setContentMode:UIViewContentModeScaleAspectFill]; 
     NSLog(@"BG Image size %f, %f", bgImage.frame.size.width, bgImage.frame.size.height); 


     UIImageView *contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(2.0, 2.0, frame.size.width-4.0, frame.size.width-4.0)]; 
     [contentImage setContentMode:UIViewContentModeScaleAspectFill]; 
     [contentImage setClipsToBounds:YES]; 
     self.image = contentImage; 

     [self.contentView addSubview:self.image]; 

     [self.contentView addSubview:bgImage]; 

     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2.0, frame.size.width, frame.size.width - 4.0, 21.0)]; 
     [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:11.0]]; 
     [label setTextAlignment:NSTextAlignmentCenter]; 
     [label setBackgroundColor:[UIColor clearColor]]; 
     self.lblName = label; 
     [self.contentView addSubview:self.lblName]; 
    } 
    return self; 
} 

UICollectionViewCell तो कोड में, bgImage 67 x 67 होने के लिए हमेशा होना चाहिए तथा इसके मूल है, 67 x 100 का एक आकार की है (0,0:

यहाँ कोड है) और सामग्री छवि का फ्रेम होना चाहिए (0,0,63,63)। डिबगिंग करके, यह सही लगता है। हालांकि, bgImage से हमेशा conentimage बड़ा है। हालांकि छवि का मूल आकार 80 x 80 है। मैंने setClipToBounds, किसी भी कक्ष पर setContentViewMode को आजमाया है। सामग्री दृश्य या छवि दृश्य, लेकिन कोई भी काम नहीं करता है।

समस्या के बारे में एक स्क्रीनशॉट संलग्न है। Image in UIImage view out of bounds

किसी भी मदद की सराहना की जाती है।

+0

मैंने निब में और स्टोरीबोर्ड में सभी तत्वों को लेआउट करने का भी प्रयास किया, वही परिणाम प्राप्त करें। – Zhao

उत्तर

1

आप 2 बार प्रयोग कर रहे हैं पर एक दूसरे

संपादित frame.size.height के बजाय frame.size.width, इस प्रयास करें:

सेल में

, जब initWithFrame तरीकों का उपयोग कर, स्वयं का उपयोग सेल की संपत्ति। आप सीमा के अंदर imageView CGRectInset विधि का उपयोग छोटे सीमा के साथ imageView प्रारंभ करने और सेल

+0

जो वास्तव में मैं चाहता था, छवि को समान चौड़ाई और ऊंचाई बनाने के लिए, जो सेल की चौड़ाई के समान है। – Zhao

+0

यह काम करता है। बहुत बहुत धन्यवाद। :) मैंने बदल दिया '' UIImageView * contentImage = [[UIImageView alloc] initWithFrame करने के लिए;: 'UIImageView * contentImage = [CGRectMake (2.0, 2.0, frame.size.width-4.0, frame.size.width-4.0) [UIImageView alloc] initWithFrame] : CGRectInset (bgImageView.bounds, 6, 6)]; 'और जादू होता है। मुझे यह पता लगाने की जरूरत है कि इन दोनों के लिए क्या अंतर है। 4.0 या 6.0 कोई फर्क नहीं पड़ता, सिर्फ 6.0 मेरे ऐप के लिए सबसे अच्छा है। – Zhao

0

UIViewContentModeScaleAspectFill को UIViewContentModeScaleAspectFit में बदलने का प्रयास करें।

+0

मैंने भरने और फिट दोनों की कोशिश की और कोई किस्मत नहीं। बस फिर कोशिश की, काम नहीं कर रहा :( – Zhao

23

यह अब तक इस सवाल का जवाब मिल गया है हो सकता है की contentView के रूप में ही करने के लिए imageviews केन्द्र स्थापित init हैं, की कोशिश:

contentImage.clipsToBounds = YES;