Czy możliwe jest wyświetlenie obrazu w UIAlertView?

Czy możliwe jest dodanie obrazu w UIAlertView, jak pokazanie obrazu z pliku plist?

Author: Anbu.karthik, 2010-02-24

9 answers

Możesz to zrobić tak:

UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];

    NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"smile.png"]];
    UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
    [imageView setImage:bkgImg];

    [successAlert addSubview:imageView];

    [successAlert show];

Spowoduje to dodanie obrazu w prawym rogu alertview możesz zmienić obraz ramki, aby się poruszać.

Mam nadzieję, że to pomoże.
 42
Author: Madhup Singh Yadav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-18 14:20:22

W systemie iOS 7 lub nowszym użyj tego kodu

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: [email protected]\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];
 10
Author: Quang Minh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-02 10:36:27

Będziesz musiał podklasować UIAlertView i nieco zmienić jego podview. Istnieje kilka samouczków do tego typu rzeczy:

 5
Author: Rob Napier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-24 04:13:08
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"your Title" message:@"Your   Message" delegate:nil cancelButtonTitle:@"Your Title" otherButtonTitles:nil];

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 40, 40)];

NSString *loc = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Your Image Name"]];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:loc];
[image setImage:img];

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [Alert setValue:image forKey:@"accessoryView"];
}else{
    [Alert addSubview:image];
}

[Alert show];
 5
Author: Anbu.karthik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-21 09:51:40
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: [email protected]\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];
 2
Author: user9235122,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-18 16:13:43

Zrobiłem modyfikację rozwiązania dostarczonego przez Madhup.

Rozwiązanie z Madhup działa dobrze dla krótkich wiadomości, jednak, gdy wiadomość jest zbyt długa, wiadomość zostanie pokryta obrazem.

Dlatego dodałem następujące kroki w metodzie UIAlertViewDelegate - (void)willPresentAlertView: (UIAlertView *)alertView

  1. Dodaj 8" \N " jako prefiks wiadomości, aby przesunąć wiadomość w dół, rezerwowanie miejsca na obraz (mój obraz był ograniczony w 100x150)

  2. Wykryj podview AlertView, aby dowiedzieć się, czy UITextView istnieje.

    UITextView będzie istnieć tylko wtedy, gdy wiadomość jest zbyt długa.

  3. Jeśli UITextView nie istnieje, wszystko będzie dobrze, obraz pokazano dobrze, wiadomość pokazano dobrze.

  4. Jeśli UITextView istnieje, usuń 8 przedrostek "\n " z UITextView.tekst, a następnie wywołaj UITextView.ustaw ramkę, aby zmienić rozmiar i zmienić pozycję UITextview.

The powyższe działanie działa dobrze.

Wysyłam NSDictionary jako wiadomość, która ma być pokazana, Słownik zawiera 2 pary klucz-wartość,"msg" = > real message string. "url" = > jako obrazek ze strony www.

Przy użyciu metody nsurlconnection sendSynchronousRequest kod po drodze pobierze dane obrazu z Internetu.

- (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
Author: Dennies Chang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-30 06:01:16

Uwaga dla IOS 7 i nowszych

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [alert setValue:imageView forKey:@"accessoryView"];
}else{
    [alert addSubview:imageView];
}
 0
Author: Ryan Heitner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-13 07:56:50

Wersja Swift:

    let alertView = UIAlertView(title: "Alert", message: "Alert + Image", delegate: nil, cancelButtonTitle: "OK")
    let imvImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    imvImage.contentMode = UIViewContentMode.Center
    imvImage.image = UIImage(named: "image_name")
    alertView.setValue(imvImage, forKey: "accessoryView")
    alertView.show()
 0
Author: Nguyễn Ngọc Bạn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-11 05:05:40

Użycie czystego układu pod:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello"
                                                                         message:nil
                                                                  preferredStyle:UIAlertControllerStyleAlert];
UIImage *image = // target image here;
CGSize size = image.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, size.width, size.height)];
imageView.image = image;
[alertController.view addSubview:imageView];
[imageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:leftMargin];
[imageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:topMargin];
[imageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:rightMargin];
[imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:bottomMargin];
[imageView autoSetDimension:ALDimensionWidth toSize:size.width];
[imageView autoSetDimension:ALDimensionHeight toSize:size.height];
// add desired actions here
[self presentViewController:alertController animated:YES completion:nil];
 -1
Author: user1232690,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-06 12:28:12