中的照片庫照片創建照片滑塊我想要照片滑塊像whts應用程序。它應該訪問構建照片庫中的所有圖片或某些圖片,並且應該使用滾動視圖進行滾動。需要使用iphone
我試過用不同的照片瀏覽器,但一切都有內存問題。所以不知道是否有人有它的工作解決方案。非常感謝您的幫助。
謝謝
安基塔
中的照片庫照片創建照片滑塊我想要照片滑塊像whts應用程序。它應該訪問構建照片庫中的所有圖片或某些圖片,並且應該使用滾動視圖進行滾動。需要使用iphone
我試過用不同的照片瀏覽器,但一切都有內存問題。所以不知道是否有人有它的工作解決方案。非常感謝您的幫助。
謝謝
安基塔
您可以使用CATiledLayer來繪製圖像。 CATiledLayer可用於通過高分辨率圖像或大量照片來提高分頁,平移和縮放的性能。
正如我上面評論你可以通過可重複使用的視圖方法意味着一次只有三個imagview或視圖將在內存中。假設您正在顯示照片編號:2,那麼您應該在照片記憶中保留照片:1,2和3(即避免閃爍),無需一次加載所有照片。假設你正在滾動照片編號:3,那麼你的邏輯應該丟棄照片編號:1並且應該加載照片編號:2,3,4。如果你還沒有得到或不知道如何以編程的方式做到這一點,那麼不要擔心。 Apple提供的很好的示例代碼可以滿足您的所有需求。
在上述示例代碼它們已經顯示顯示圖像的雙向的。
一個是直接顯示/設置圖像到圖像視圖。
二是與使用該方法TileView繪製圖像:- (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize
如果你正在考慮很多關於內存的話,肯定你必須使用第二種方法。
這是工作得很好,但與backbord方向滾動一個小問題。試試這可能是你可以找到一些方向去使用它可能會找到更好的解決方案。
(曾用ELCImagePicker顯示第一次的所有圖像,然後選擇一個本滾動視圖在10個圖像所示的時間也有調整ELCImagePickerController的委託方法來獲得所選圖像的索引顯示放大圖)
在第一負載即,當從ELCImagePickerController
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info selectedIndex:(NSInteger)_index {
[self dismissModalViewControllerAnimated:NO];
for (UIView *v in [scrollview subviews]) {
[v removeFromSuperview];
}
infoItems = [[NSArray alloc]initWithArray:info];
CGRect workingFrame = scrollview.frame;
workingFrame.origin.x = 0;
int indexFrame = picker.selectedIndex;
newIndex = indexFrame;
for(int i = 0; i < [info count]; i++)
{
NSDictionary *dict = [info objectAtIndex:i];
UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
imageview.tag = i;
if(i >= indexFrame && i <= indexFrame + 9)
{
[scrollview addSubview:imageview];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
}
[scrollview setPagingEnabled:YES];
[scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
[scrollview scrollRectToVisible:CGRectMake(0, 0, workingFrame.size.width, workingFrame.size.height) animated:NO];
self.scrollview.hidden = NO;
self.pageControll.hidden = NO;
self.pageControll.numberOfPages = [[scrollview subviews]count];
index = 0;
lastContentOffset = scrollview.contentOffset.x;
NSLog(@"lastContentOffset %.2f",lastContentOffset);
}
選擇圖像之後,關於scrollViews委託方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"ScrollViewDidEndDecelerating is called with subview(s) %d",[[scrollview subviews]count]);
if(lastContentOffset < scrollView.contentOffset.x)
{
index += (int)(scrollview.contentOffset.x - lastContentOffset)/scrollview.frame.size.width;
newIndex += (int)(scrollview.contentOffset.x - lastContentOffset)/scrollview.frame.size.width;
self.pageControll.currentPage = index;
NSLog(@"Index incremented %d \n newIndex %d",index,newIndex);
if(index == [[scrollView subviews]count]-1)
{
if(newIndex < [infoItems count]-1)
{
[self performSelector:@selector(reloadScrollViewWithNewImages)];
}
}
[Appirater userDidSignificantEvent:YES];
}
else if(lastContentOffset > scrollView.contentOffset.x)
{
index -= (int)(lastContentOffset - scrollview.contentOffset.x)/scrollview.frame.size.width;
newIndex -= (int)(int)(lastContentOffset - scrollview.contentOffset.x)/scrollview.frame.size.width;
self.pageControll.currentPage = index;
NSLog(@"Index decremented %d \n newIndex %d",index,newIndex);
if(index == 0)
{
if(newIndex > 0)
{
newIndex -= 9;
if(newIndex < 0)
newIndex = 0;
[self performSelector:@selector(reloadScrollViewWithNewImages)];
}
}
[Appirater userDidSignificantEvent:YES];
}
lastContentOffset = scrollView.contentOffset.x;
NSLog(@"New lastContentOffset %.2f",lastContentOffset);
}
並用於重載滾動視圖是遵循
-(void)reloadScrollViewWithNewImages
{
for (UIView *v in [scrollview subviews]) {
[v removeFromSuperview];
}
CGRect workingFrame = scrollview.frame;
workingFrame.origin.x = 0;
NSLog(@"reloadScrollView newIndex %d",newIndex);
int indexFrame = newIndex;
for(int i = 0; i < [infoItems count]; i++)
{
NSDictionary *dict = [infoItems objectAtIndex:i];
UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
imageview.tag = i;
if(i >= indexFrame && i <= indexFrame + 9)
{
[scrollview addSubview:imageview];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
}
[scrollview setPagingEnabled:YES];
[scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
[scrollview scrollRectToVisible:CGRectMake(0, 0, workingFrame.size.width, workingFrame.size.height) animated:NO];
self.scrollview.hidden = NO;
self.pageControll.hidden = NO;
index = 0;
self.pageControll.numberOfPages = [[scrollview subviews]count];
NSLog(@"number %d",[[scrollview subviews]count]);
lastContentOffset = scrollview.contentOffset.x;
NSLog(@"reloadScrollView's lastContentOffset %.2f",lastContentOffset);
}
,所有使用的屬性和聲明爲private實例變量的方法。在此代碼所需的.h文件的接口部分是
私人的ivars
NSInteger index;
float lastContentOffset;
屬性
@property (nonatomic,strong) IBOutlet UIScrollView *scrollview;
@property (strong, nonatomic) NSArray *infoItems;
@property (assign, atomic) int newIndex;
編碼快樂:)這裏
同樣的問題了。如果需要更多圖像,必須將圖像限制爲某個數字,例如15如果使用scrollview的委託來加載更多圖像,並使用在創建圖像視圖時設置的索引屬性來刪除圖像 – 2012-08-01 12:10:43
@AalokParikh:那麼它是否工作完美?如果是的話,你能分享你的代碼嗎? – Aks 2012-08-01 12:13:42
工作完美,差不多。但是,現在無法共享它,因爲我在家中的Windows PC上,目前沒有可用的代碼。並且也要分享它,我也要問我的項目經理。 :( – 2012-08-01 12:18:01