0

我有一个小问题,我试图在用户单击时生成 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文件中合成。

4

2 回答 2

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;
}

}
于 2013-01-31T13:36:10.210 回答
0

在添加新的图像视图之前,您必须删除以前的图像。像这样的东西应该工作......

if (self.qrcodeImageView && self.qrcodeImageView.superview)
    [self.qrcodeImageView removeFromSuperview];
self.qrcodeImageView = [[UIImageView alloc] initWithImage:qrcodeImage];
于 2013-01-31T13:03:15.063 回答