UIImageView图像彼此重叠

时间:2013-01-31 12:21:03

标签: iphone ios uiimageview uiimage

我遇到一个小问题,我试图在用户点击时生成QR码,从而从UITextField获取字符串。一切都很好,除了一个新的UIImageView重叠另一个,尽管我正在重新初始化它。

他的职能:

- (void)showQR
{
    if([textFieldRounded.text length] != 0 ){
        NSString* aVeryLongURL = textFieldRounded.text;
        DataMatrix* qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_H version:QR_VERSION_AUTO string:aVeryLongURL];
        qrcodeImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrcodeImageDimension];
        qrcodeImageView = [[UIImageView alloc] initWithImage:qrcodeImage];
        CGRect parentFrame = self.view.frame;
        CGRect tabBarFrame = self.tabBarController.tabBar.frame;
        CGFloat x = (parentFrame.size.width - qrcodeImageDimension) / 2.0;
        CGFloat y = (parentFrame.size.height - qrcodeImageDimension - tabBarFrame.size.height) / 2.0;
        y = y+75;
        CGRect qrcodeImageViewFrame = CGRectMake(x, y, qrcodeImageDimension, qrcodeImageDimension);
        [qrcodeImageView setFrame:qrcodeImageViewFrame];
        [self.view addSubview:qrcodeImageView];
        return;
    }
    else
    {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Enter the URL" delegate:nil cancelButtonTitle: @"OK" otherButtonTitles:nil];
        [alert1 show];
        return;
    }

}

每次用户点击调用此功能的提交按钮时,新的QR与旧的QR重叠。

我有我的.h文件,我声明了

@property (atomic, strong)  UIImageView *qrcodeImageView;
@property (atomic, strong)  UIImage* qrcodeImage;

也在.m文件中合成。

2 个答案:

答案 0 :(得分:0)

在添加新的imageview之前,您必须删除上一张图像。 这样的事情应该有用......

if (self.qrcodeImageView && self.qrcodeImageView.superview)
    [self.qrcodeImageView removeFromSuperview];
self.qrcodeImageView = [[UIImageView alloc] initWithImage:qrcodeImage];

答案 1 :(得分:0)

这会有效,试试这个

- (void)showQR
{

if([textFieldRounded.text length] != 0 ){
    NSString* aVeryLongURL = textFieldRounded.text;
    DataMatrix* qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_H version:QR_VERSION_AUTO string:aVeryLongURL];
    qrcodeImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrcodeImageDimension];


    if (qrcodeImageView && [qrcodeImageView superview] && qrcodeImage!=nil ) {
        qrcodeImageView.image = qrcodeImage;
        return;
    }


    qrcodeImageView = [[UIImageView alloc] initWithImage:qrcodeImage];
    CGRect parentFrame = self.view.frame;
    CGRect tabBarFrame = self.tabBarController.tabBar.frame;
    CGFloat x = (parentFrame.size.width - qrcodeImageDimension) / 2.0;
    CGFloat y = (parentFrame.size.height - qrcodeImageDimension - tabBarFrame.size.height) / 2.0;
    y = y+75;
    CGRect qrcodeImageViewFrame = CGRectMake(x, y, qrcodeImageDimension, qrcodeImageDimension);
    [qrcodeImageView setFrame:qrcodeImageViewFrame];
    [self.view addSubview:qrcodeImageView];
    return;
}
else
{
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Enter the URL" delegate:nil cancelButtonTitle: @"OK" otherButtonTitles:nil];
    [alert1 show];
    return;
}

}