22

plistファイルから画像を表示するように、UIAlertViewに画像を追加することは可能ですか?

4

9 に答える 9

43

次のようにできます。

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

これにより、アラートビューの右隅に画像が追加され、フレーム画像を変更して移動できます。

お役に立てれば。

于 2010-02-24T04:39:02.503 に答える
10

iOS 7 以降では、このコードを使用します

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];
于 2015-01-02T10:36:27.133 に答える
5

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

于 2010-02-24T04:13:08.363 に答える
5
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];
于 2013-10-03T05:16:49.733 に答える
0

Madhup が提供するソリューションにいくつかの変更を加えました。

Madhup のソリューションは、短いメッセージには問題なく機能しますが、メッセージが長すぎると、メッセージが画像で隠れてしまいます。

したがって、UIAlertViewDelegate のメソッドに次の手順を追加しました - (void)willPresentAlertView:(UIAlertView *)alertView

  1. メッセージのプレフィックスとして 8 "\n" を追加して、メッセージを押し下げ、画像用のスペースを確保します (私の画像は 100x150 に制限されていました)

  2. AlertView のサブビューを検出して、UITextView が存在するかどうかを調べます。

    UITextView は、メッセージが長すぎる場合にのみ存在します。

  3. UITextView が存在しない場合、すべて問題なく、画像は適切に表示され、メッセージは適切に表示されます。

  4. UITextView が存在する場合は、UITextView.text から 8 プレフィックス "\n" を削除し、UITextView.setFrame を呼び出してサイズ変更し、UITextview の位置を変更します。

上記のアクションは正常に機能します。

表示するメッセージとして NSDictionary を送信します。辞書には 2 つのキーと値のペア、"msg"=>実際のメッセージ文字列が含まれています。"url"=>Web サイトの画像として。

NSURLConnection sendSynchronousRequest メソッドを使用すると、コードは途中でインターネットから画像データを取得します。

- (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];
            }
        }        
    }
}
于 2012-03-29T06:07:43.770 に答える
0

IOS 7 以降の注意事項

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [alert setValue:imageView forKey:@"accessoryView"];
}else{
    [alert addSubview:imageView];
}
于 2014-11-13T07:56:50.533 に答える
-1

純粋なレイアウト ポッドの使用:

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];
于 2017-03-06T12:28:12.197 に答える