需要使用iPhone中的照片库照片创建照片滑块 [英] Need to create photo slider using photo library photos in iphone

查看:94
本文介绍了需要使用iPhone中的照片库照片创建照片滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我尝试使用其他照片查看器,但是所有内容都存在内存问题.因此,不确定是否有人可以使用它.非常感谢您的帮助.

谢谢

安吉塔

解决方案

这几乎可以正常工作,但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);
}

以及用于重新加载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是

私人头像

NSInteger index;
float lastContentOffset;

属性

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

快乐编码:)

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. 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)

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

-(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

-(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

Private ivars

NSInteger index;
float lastContentOffset;

Properties

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

Happy Coding :)

这篇关于需要使用iPhone中的照片库照片创建照片滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆