需要使用iphone中的照片库照片创建照片滑块

时间:2012-08-01 11:41:38

标签: iphone xcode photo alassetslibrary

我想像应用程序的照片滑块。它应该访问构建照片库中的所有图片或一些图片,它应该可以使用滚动视图滚动。

我尝试过不同的照片浏览器,但一切都有内存问题。所以不确定是否有人有它的工作解决方案。真的很感谢你的帮助。

谢谢,

安基塔

2 个答案:

答案 0 :(得分:1)

这工作得很好,但背板方向滚动有一个小问题。试试这个可能是你可以找到一些方向去,可能会找到更好的解决方案。

(已经使用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);
}

用于重新加载scrollView的方法如下

-(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);
}

此代码中所需的.h文件接口部分中声明的所有已使用属性和私有ivars都是

私人ivars

NSInteger index;
float lastContentOffset;

属性

@property (nonatomic,strong) IBOutlet UIScrollView *scrollview;
@property (strong, nonatomic) NSArray *infoItems;
@property (assign, atomic) int newIndex;

快乐编码:)

答案 1 :(得分:0)

您可以使用CATiledLayer绘制图像。 CATiledLayer可用于提高使用高分辨率图像或大量照片进行分页,平移和缩放的性能。

正如我在上面评论过的那样,你可以通过可重复使用的视图方法,一次只有三个想象或视图将在内存中。假设您要显示照片编号:2因此,您必须保留照片:1,2和3在内存中(即避免闪烁)无需一次加载所有照片。假设您正在滚动到照片编号:3然后您的逻辑应该丢弃照片编号:1并且应该加载照片编号:2,3,4。如果您仍然没有获得或不知道如何以编程方式执行此操作,请不要担心。 Apple提供了很好的示例代码,可以满足您的所有需求。

Sample code

在上面的示例代码中,他们显示了两种显示图像的方式。
一个是直接显示/设置图像到图像视图。
其次是使用此方法使用TileView绘制图像:
- (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize

如果你正在考虑很多关于记忆的话,那么你肯定必须使用第二种方法。