简体   繁体   English

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

[英]Need to create photo slider using photo library photos in iphone

I want photo slider like whts app has. 我想要像应用程序一样的照片滑块。 It should access all the pics or some of the pics from in build photo library and it should be scrollable using scroll view. 它应该访问构建图片库中的所有图片或某些图片,并且应该可以使用滚动视图滚动。

I have tried with different photo viewer but everything has memory issue. 我尝试使用其他照片查看器,但是所有内容都有内存问题。 So not sure if anyone has it's working solution. 因此,不确定是否有人可以使用它。 Really appreciate your help. 非常感谢您的帮助。

Thank you, 谢谢,

Ankita 安基塔

This is working almost fine, but one small problem with backbord direction scrolling. 这几乎可以正常工作,但是backbord方向滚动有一个小问题。 Just try this out may be you can find some direction to go and may be find better solution using it. 只需尝试一下,可能会找到一些方向,并可能会找到更好的解决方案。

(Have used ELCImagePicker to show first time all images and then on selecting one present the scrollview to show large images at a time 10 images are shown also have tweak the delegate method of ELCImagePickerController to get the index of selected image) (已经使用ELCImagePicker来第一次显示所有图像,然后在选择一个当前呈现的滚动视图时一次显示大图像,同时显示了10张图像,还调整了ELCImagePickerController的委托方法来获取所选图像的索引)

At first load ie when select image from 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);
}

After that on scrollViews delegate method 之后,在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);
}

and the method used for reload the scrollView is as follow 并且用于重新加载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);
}

and all used properties and private ivars declared in .h file's interface part required in this code are 并且此代码所需的.h文件的接口部分中声明的所有使用的属性和私有ivars是

Private ivars 私人ivars

NSInteger index;
float lastContentOffset;

Properties 性质

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

Happy Coding :) 快乐编码:)

You can use CATiledLayer to draw image. 您可以使用CATiledLayer绘制图像。 CATiledLayer can be used to increase the performance of paging, panning, and zooming with high-resolution images or large sets of photos. CATiledLayer可用于提高高分辨率图像或大量照片的分页,平移和缩放的性能。

As i commented above you can go by reusable view approach means at a time only three imagview or view will be in memory. 正如我上面评论的那样,您可以通过可重用的视图方法进行操作,一次内存中将只有三个imagview或view。 Let say you are showing photo number:2 so, you should have to keep photo:1,2 and3 in memory(ie to avoid flicker) no need to load all photos at a time. 假设您显示的是照片编号2,因此,您必须将照片:1,2和3保留在内存中(即避免闪烁),而无需一次加载所有照片。 Suppose you are scrolling to photo number:3 then your logic should discard photo number:1 and should load photo number:2,3,4. 假设您要滚动到照片编号:3,那么您的逻辑应该丢弃照片编号:1,并应加载照片编号:2、3、4。 If you are still not getting or don't know how to do this programmatically then don't worry. 如果您仍然不了解或不知道如何以编程方式执行此操作,请不要担心。 Apple's good sample code is available that is doing all you want. 苹果提供了很好的示例代码,可以满足您的所有需求。

Sample code 样例代码

In above sample code they have displayed two way of displaying image. 在上面的示例代码中,他们显示了两种显示图像的方式。
One is directly showing/setting the image into image view. 一种是直接将图像显示/设置为图像视图。
Second is with drawing image with TileView using this method: 其次是使用此方法通过TileView绘制图像:
- (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize

If you are thinking lot of about memory then definitely you have to use second approach. 如果您考虑大量内存,那么绝对必须使用第二种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM