0

Imagepickerviewコントローラーを使用して写真を選択し、imageviewを使用してuiviewに表示しましたが、私の問題はimageviewを使用して2つではなく1つの画像しか表示できないことです。2番目のものを選択すると、既存のものを置き換え続けます。2枚の写真を表示する方法を教えてください。

here is my source code.


-(void) ViewDidLoad
{
      attachPhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    attachPhotoBtn.frame = CGRectMake(400, 125, 44, 44);
    UIImage *attachImg = [UIImage imageNamed:@"album_add_off.png"];
    [attachPhotoBtn setImage:attachImg forState:UIControlStateNormal];
    [attachPhotoBtn addTarget:self action:@selector(attachPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:attachPhotoBtn];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 125, 64, 52)];
//        imageView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:imageView];
    }

}


- (IBAction)attachPhoto:(id)sender {

    [sender setImage:[UIImage imageNamed:@"album_add.png"] forState:UIControlStateNormal];

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =[[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,(NSString *)kUTTypeVideo,nil];
        imagePicker.allowsEditing = NO;

        // On iPad use pop-overs.
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            _popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
            [_popover presentPopoverFromRect:attachPhotoBtn.frame
                                          inView:self.view
                        permittedArrowDirections:UIPopoverArrowDirectionUp
                                        animated:YES];
            }
        }
        else
        {
            // On iPhone use full screen presentation.

            // [[self presentingViewController] presentViewController:imagePicker animated:YES completion:nil];
        }


        newMedia = NO;
    }


#pragma mark Image picker controller delegate methods

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

//    [self dismissModalViewControllerAnimated:YES];


    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        imageView.image = image;



    [picker dismissViewControllerAnimated:NO completion:nil];
}
4

2 に答える 2

2

画像を表示するために単一の UIImageView コンポーネントを使用しています。画像を選択するたびに、最新の画像によって上書きされます..

複数の画像を表示する方法は複数あります。

  1. (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info関数で画像を取得すると、動的に UIImageView コンポーネントを作成できます。

  2. NSMutableArray を使用してすべての画像を配置し、UITableView に表示できます。

  3. または、ScrollView にイメージビューを追加することもできます。

ただし、最終的には、1 つではなく複数の画像を表示するには、複数の UIImageView コンポーネントが必要になります。

お役に立てれば。

于 2013-02-05T07:01:58.207 に答える
0

アセット ライブラリを使用して、フォト ライブラリからすべての画像を取得できます。

1. AssetsLibrary.framework を追加します

2. #import "AssetsLibrary/AssetsLibrary.h"

3. すべての結果を配列で取得する

ここにコードがあります

void (^myAssetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != NULL)
        {
            NSLog(@"See Asset: %@", result);

            if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
            {
                [assetsArray addObject:result];
            }
        }
    };

    //This block of code used to enumerate ALAssetsGroup.
    void (^myAssetGroupEnumerator)( ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:myAssetEnumerator];

            [self createScrollView];//Or you can use your `assetsArray` data, in UITableView,UICollectionView 

        }
        //[activity stopAnimating];
        // [activity setHidden:YES];
    };

    library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                           usingBlock:myAssetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }
     ];

4. サムネイル画像を取得する

ALAsset *asset=[assetsArray objectAtIndex:i]; 
UIImage*image = [UIImage imageWithCGImage:[asset thumbnail]];
于 2013-02-05T07:09:32.600 に答える