Is it possible to show an Image in UIAlertView?

后端 未结 9 427
别跟我提以往
别跟我提以往 2020-11-29 02:01

Is it possible to add image in an UIAlertView, like showing an image from the plist file?

相关标签:
9条回答
  • 2020-11-29 02:37
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 282)];
    UIImage *wonImage = [UIImage imageNamed:@"iberrys.png"];
    imageView.contentMode = UIViewContentModeCenter;
    [imageView setImage:wonImage];
    UIAlertView *alertView = [[UIAlertView alloc]  initWithTitle:@"Arirang List"
                                                         message:@"phiên bản: 1.0\n website: www.iberrys.com\n email: quangminh@berrys.com\nmobile: 0918 956 456"
                                                        delegate:self
                                               cancelButtonTitle:@"Đồng ý"
                                               otherButtonTitles:nil];
    //check if os version is 7 or above
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [alertView setValue:imageView forKey:@"accessoryView"];
    } else {
        [alertView addSubview:imageView];
    }
    [alertView show];
    
    0 讨论(0)
  • 2020-11-29 02:40

    You'll need to subclass UIAlertView and rearrange its subviews a bit. There are several tutorials for this kind of stuff:

    • Custom UIAlertView (probably the most applicable to your problem)
    • Custom UIAlertView w/ TableView (also very handy)
    0 讨论(0)
  • 2020-11-29 02:46

    I did some modification for the solution provided by Madhup.

    The solution from Madhup work fine for short message, however, when the message is too long, message will be covered by image.

    Hence, I added the following steps in method of UIAlertViewDelegate - (void)willPresentAlertView:(UIAlertView *)alertView

    1. Add 8 "\n" as prefix of message, to push the message down, reserving space for image (my image was restricted in 100x150)

    2. Detect the subviews of AlertView, to find out if UITextView exists.

      UITextView will exist only when the message is too long.

    3. If the UITextView does not exist, everything will be fine, image shown good, message shown good.

    4. If the UITextView exists, remove the 8 prefix "\n" from UITextView.text, and then call UITextView.setFrame to resize and change the position of the UITextview.

    The above action works fine.

    I send an NSDictionary as the message to be shown, the dictionary contains 2 key-value pairs, "msg"=>real message string. "url"=>as the image from web site.

    With the NSURLConnection sendSynchronousRequest method, the code will retrieve image data from internet on the way.

    - (void)showAlertView:(NSDictionary *)msgDic {
        NSLog(@"msgDic = %@", msgDic);
        NSMutableString *msg = [[NSMutableString alloc] initWithString:@"\n\n\n\n\n\n\n\n"];
        if ([msgDic objectForKey:@"msg"]) {
            [msg appendFormat:@"%@", [msgDic objectForKey:@"msg"]];
        }
        else {
            [msg setString:[msgDic objectForKey:@"msg"]];
        }
    
        NSLog(@"msg = %@", msg);
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert Title"
                                                    message:msg
                                                   delegate:self
                                        cancelButtonTitle:@"Close" otherButtonTitles:nil];
    
         if ([msgDic objectForKey:@"url"]) {
             NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[msgDic objectForKey:@"url"]]];
             [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    
             NSData *imgData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
             if (imgData) {
                 UIImage *shownImage = [UIImage imageWithData:imgData];
                 UIImageView *imgView = [[UIImageView alloc] initWithImage:shownImage];
                 [imgView setFrame:CGRectMake(floor(284-100)/2.0, 47, 100, 150)];
    
                 [alert addSubview:imgView];
                 [imgView release];
             }
         }
    
        alert.delegate = self;
        [alert show];
        [alert release];
        [msgDic release];
    }
    
    - (void)willPresentAlertView:(UIAlertView *)alertView {
    int viewCount = [alertView.subviews count];
    
        NSLog(@"subviews count = %i", viewCount);
    
        if (viewCount > 0) {
            BOOL bFoundTextView = NO;
            for (int count=0; count<=[alertView.subviews count] -1; count++) {
                BOOL bIsTextView = NO;
                UIView *subView = [alertView.subviews objectAtIndex:count];
                NSLog(@"view index %i classname = %@", count, [[subView class] description]);
    
                bIsTextView = [[[subView class] description] isEqualToString:@"UIAlertTextView"];
                bFoundTextView |= bIsTextView;
    
                if (bIsTextView) {
                    UITextView *textView = (UITextView *)subView;
                    NSMutableString *msg = [[NSMutableString alloc] initWithString:textView.text];
                    [msg setString:[msg substringFromIndex:8]];
                    textView.text = msg;
    
                    CGRect frame = textView.frame;
                    if (frame.origin.y != 205) {
                        frame.origin.y = 205;
                        frame.size.height -= 155;
                        [textView setFrame:frame];
                    }
    
                    [msg release];
                }
            }        
        }
    }
    
    0 讨论(0)
提交回复
热议问题