簡體   English   中英

如何將圖像從保存的相冊添加到收藏夾視圖單元格中?

[英]How to add image into collection view cell from saved photo albums?

我必須制作一個類似於iPhone中的照片應用程序的照片庫。 我包括了一個收集視圖以顯示圖像。 當我單擊“插入圖像”按鈕時,將顯示“照片”應用程序的已保存相冊(使用UIImagePicker )。 但是我無法將圖像加載回自定義collectionViewCell CollectionViewCell包含一個UIImageView

您的collectionView的cellForItemAtIndexPath應該看起來像這樣。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //create cel...
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    //create UIImageView (declare imageView in header file)
    self.cellImageView = [[UIImageView alloc] initWithFrame:"YOUR FRAME"];
    //set image. delete this line if there's no before picking image in uipickercontroller
    [self.cellImageView setImage:"YOUR IMAGE BEFORE USING IMAGEPICKER"];
    [cell addSubView:self.cellImageView];
    return cell;
}

didFinishPickingMediaWithInfo應該看起來像這樣...

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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    //set image when image is chosen from image picker controller
    self.cellImageView = chosenImage;
    //reload collectionView
    [self.collectionView reloadData];

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

請使用ALAsset庫。

ALAsset對象代表由Photo應用程序管理的照片或視頻。

請看一下本教程系列“像Over一樣創建圖像庫 ”。

在本教程中,您將學習如何在UICollectionView中實際顯示保存的相冊。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM