我有一個UICollectionView
。在這裏,我展示了一些來自我的MainBundle
的照片。這些照片的位置或目錄保存在SQLite
數據庫中。我正在使用segue
。問題是當我嘗試發送圖像鏈接到detailsViewController
它不發送鏈接。UICollectionViewCell發送數據到另一個viewController
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
selectedPhotoIndex = indexPath.row;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
{
PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex];
[photoDetailsViewController setImagePath:pProperty.link];
//selectedPhotoIndex = 0;
}
}
但如果我把代碼出segue.identifier
其發送的鏈接detailsViewController
。
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:selectedPhotoIndex];
[photoDetailsViewController setImagePath:pProperty.link];
//selectedPhotoIndex = 0;
}
但這裏的問題是,它持有一個鏈接的參考一些地方和以顯示正確的圖像我有選擇的拇指線圖像再次挖掘。我試着用這些替代品,但沒有運氣。
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
{
PhotoDetailsViewController *photoDetailsViewController = [segue destinationViewController];
CustomCell *cell = (CustomCell *) sender;
NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell];
//NSLog(@"indexPath %@",indexPath);
PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row];
[photoDetailsViewController setImagePath:pProperty.link];
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"PhotoDetailsViewController"])
{
CustomCell *cell = (CustomCell *) sender;
NSIndexPath *indexPath = [self.photoCollectionView indexPathForCell:cell];
int num = indexPath.row %10;
NSLog(@"num %i", num);
PhotoDetailsViewController *photoDetailsViewController = (PhotoDetailsViewController *)[segue destinationViewController];
PhotoProperty *pProperty = [self.arrayOfPhotos objectAtIndex:num];
[photoDetailsViewController setImagePath:pProperty.link];
}
}
這裏是我的全碼:
- (void)viewDidLoad
{
[super viewDidLoad];
self.sQLite = [[SQLite alloc] init];
[self.sQLite callDataBaseAndPhotoTableMethods];
self.arrayOfPhotos = [[NSMutableArray alloc] init];
self.arrayOfPhotos = [self.sQLite returnDataFromPhotoTable];
self.photoCollectionView.delegate = self;
self.photoCollectionView.dataSource = self;
}
/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
/*------------ It takes the original size of photo and make it's size smaller (Down)------------*/
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.arrayOfPhotos count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
self.photoProperty = [self.arrayOfPhotos objectAtIndex:indexPath.row];
// NSLog(@"id %d", photoProperty.idNumPHOTO);
// NSLog(@"name %@", photoProperty.name);
// NSLog(@"links %@", photoProperty.link);
NSData *imageData = [NSData dataWithContentsOfFile:photoProperty.link];
UIImage *originalImage = [[UIImage alloc] initWithData:imageData];
// This is the size where I get from the Cell UIImageView hight & wodth
CGSize size = CGSizeMake(36.0f, 51.0f);
UIImage *thumbNailImage = [self imageWithImage:originalImage scaledToSize:size];
cell.imageView.image = thumbNailImage;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
如果任何一個理解我的問題,有什麼解決辦法請與我分享。先進的感謝很多。
祝您有愉快的一天。
感謝您的回答。它現在正在完美工作。 :) – Tulon