UIImageView setImage无法正常工作?

时间:2013-11-30 11:35:18

标签: ios uiimageview

我有一个类,我有两个UIImageViews。如果控制我正在设置这些ImageView的图像,但它没有设置。

我从另一个班级调用该方法。

我的代码如下,

@synthesize coordinate, title, subtitle,number,index,vstd,sold;

NSString *identifier;
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{

  self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

  if (self != nil)
    {

     UIImageView * ecz = [[UIImageView alloc]init];
     ecz.frame = CGRectMake(0, 0, 87, 50);
     ecz.image = [STCachedImage getCachedImage:@"eczane_indicator"];
     [self addSubview:ecz];
     [ecz release];

     vstd = [[UIImageView alloc]initWithFrame:CGRectMake(8, 10, 25, 22)];
     [self addSubview:vstd];
     [vstd release];

     sold = [[UIImageView alloc]initWithFrame:CGRectMake(50, 10, 25, 22)];
     [self addSubview:sold];
     [sold release];
    }

  return self;

  }

-(void) visitOrderFlag : (NSString*)visitFlag withOrderFlag:(NSString*)orderFlag
{

 if([visitFlag isEqualToString:@"0"])
  {
   UIImage *visitedImage = [STCachedImage getCachedImage:@"pharm1"];
   [vstd setImage:visitedImage];
  }
 else
 {
  UIImage *visitedPressedImage = [STCachedImage getCachedImage:@"pharm2"];
  [vstd setImage:visitedPressedImage];
 }

 if([orderFlag isEqualToString:@"0"])
 {
  UIImage *orderedImage = [STCachedImage getCachedImage:@"pharm3"];
  [sold setImage:orderedImage];
 }
 else
 {
  UIImage *orderedPressedImage = [STCachedImage getCachedImage:@"pharm4"];
  [sold setImage:orderedPressedImage];
 }

}

@end

我缺少什么?

请给我一个解决方案

2 个答案:

答案 0 :(得分:2)

使用此方法,并在视图控制器类中添加一个观察者,以观察图像对象的变化。当它通知时,您可以将您的图像对象放入UIimageView。

-(UIImage *)thumbImage{

          __block UIImage *myImage;
        if([[NSFileManager defaultManager] fileExistsAtPath:[Utility systemFilePath:imageURL_]]) {
            NSData *imageData =  [NSData dataWithContentsOfFile:[Utility systemFilePath:imageURL_]];
            if (imageData != nil) {
                dealImage = [UIImage imageWithData:imageData];
            }
        }
        else{



            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                //Load image in background
                NSData* imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString: dealImageURL_]];

                dispatch_async(dispatch_get_main_queue(), ^{

                    myImage = [UIImage imageWithData:imgData];
                    if(myImage){
                        NSString *filePathToSave = [Utility systemFilePath:imageURL_];
                        if (filePathToSave) {
                             [self willChangeValueForKey:@"profileImage"];
                             [imgData writeToFile:filePathToSave atomically:YES];
                             [self didChangeValueForKey:@"profileImage"];


                        }
                    }

                });
            });
        }

        return myImage;
    }

答案 1 :(得分:1)

 *ecz.image = [STCachedImage getCachedImage:@"eczane_indicator"];

此方法getchachedImage花费时间,之后您将释放图像视图。

相关问题