ios小demo之图片播放器第一步:加载图片功能

来源:互联网 发布:淘宝关键词修改不见 编辑:程序博客网 时间:2024/05/01 21:09


        之前学习ios程序设计的时候做了一个比较粗糙的图片播放器demo,用于练习一些ios的记本知识。不能用专门的标签概括这个demo,只能说是供和我一样的新手在初学的时候练手用的。下面是程序的大概过程。


        在编写之前,首先考虑一下,这个图片播放器大概需要有几个功能。经过短暂的考虑,能想到的功能如下:


         1,图片加载:作为图片播放器,肯定要有图片加载功能。这里以iphone上使用为例子,至少需要可以从手机相册中读取图片并显示在手机的屏幕上。其次,还可以添加其他的加载方式,比如可以直接用网络图片的url加载。

 

         实现的初始思路大概是:


          设置一个浏览的按键,按键按下后,弹出提示框,提示是从手机相册中读取图片或者是拍照直接加载。代码如下:


//一下是整个button的代码

    

- (IBAction)ScanButton:(id)sender {

    UIActionSheet *sheet;


    // 判断是否支持相机


    if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])


    {

    //若支持相机,则可以通过拍照加载,否则else

        sheet  = [[UIActionSheetalloc] initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"拍照",@"从相册选择",nil];


    }


    else {


        sheet = [[UIActionSheetalloc] initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"从相册选择",nil];


    }


    sheet.tag = 255;


    [sheet showInView:self.view];


}



           做好了这一块以后就要写action了,action毫无疑问就是跳转到拍照还是相册。不多说,代码和注释如下:


-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex


{

    

    if (actionSheet.tag ==255) {


        NSUInteger sourceType = 0;


        // 判断是否支持相机


        if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {


            switch (buttonIndex) {

                    

                case 0:

                    

                    // 取消

                    

                    return;

                    

                case 1:

                    

                    // 相机

                    

                    sourceType = UIImagePickerControllerSourceTypeCamera;

                    

                    break;


                case 2:

                    

                    // 相册

                    

                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                    

                    break;

                    

            }

        

        }

     

        else {

        

            if (buttonIndex == 0) {

     

                return;

               

            } else {

           

                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

            

            }

         

        }


        // 跳转到相机或相册页面


        UIImagePickerController *imagePickerController = [[UIImagePickerControlleralloc] init];


        imagePickerController.delegate = self;


        imagePickerController.allowsEditing =YES;


        imagePickerController.sourceType = sourceType;


        [self presentViewController:imagePickerControlleranimated:YES completion:^{}];

        //最后一句实现跳转

      

    }

  

}



        写完以上的action之后,点击拍照或者相册就会有响应了,跳转到对应的拍照或者相册界面。拍完或者选择完相册的图片之后。我们还要把图片显示到屏幕上,为了达到图片播放的效果,当然要把每次加载的图片储存起来。这里可以用一个数组来实现。NSMutableArray定义一个数组,用于存储图片。通过

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage] 可以读取刚才拍照或者选择的图片。下面是各种参数值对应的功能。这里选择原始图片。

    

    /* 此处info 有六个值

     08

     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)

     09

     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片

     10

     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片

     11

     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)

     12

     * UIImagePickerControllerMediaURL;       // an NSURL

     13

     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework

     14

     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo

     15

     */





    具体的代码如下:(这个是选择完以后响应的函数)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info   


{    [picker dismissViewControllerAnimated:YEScompletion:^{}];


    UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];

    


    // 保存图片至本地,方法见下文

    

    [selfsaveImage:image withName:@"currentImage.png"];


    NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"currentImage.png"];


    UIImage *savedImage = [[UIImagealloc] initWithContentsOfFile:fullPath];


    _isFullScreen = NO;


    [imageArray addObject:savedImage];

    

    //把图片放到一个数组里面

    

    [imageViewsetImage:[imageArrayobjectAtIndex:i]];


    front = i;

    

    i++;

    

    

    imageView.tag =100;


}




        仅仅是图片加载,有点太限制了。我们决定实现一个网络图片url直接加载功能。这个功能也很简单。很粗糙的写,可以直接写一个request,把url填上去,把图片数据下载下来,然后直接把图片加载进数组里面即可。


         这里比较简单,直接贴代码:

  

         

- (IBAction)SureButton:(id)sender {

    [PictureUrlresignFirstResponder];          //这里有一个text用来纪录用户填写的图片url

    URLString = self.PictureUrl.text;              //保存这个url,用来下载

    

    //NSLog(@"url = %@",URLString);

    

    

    NSURL *url = [[NSURLalloc]initWithString:URLString];

    

    NSData *data = [NSDatadataWithContentsOfURL:url];

    

    UIImage *image = [[UIImagealloc] initWithData:data];

     //这里说明一下,这种方法是非常粗糙的,没有下载完的话还不能执行其他操作,如果网络不是很好,这样子加载会比较卡。还有一种方法是用GCD的方式下载,这里不讨论这种方法,有兴趣的朋友可以自己找一下GCD的方式改进这部分。ps:这是中非常有用的方法,学习ios必须学会这个。

    [self.imageViewsetImage:image];

    [imageArray addObject:image];        //加载进数组里面

    //NSLog(@"到这里了!");

    front = i;

    i++;


    [urlappear setText:PictureUrl.text];

    PictureUrl.text =NULL;                 //清除text的内容以便下次加载

}

         


            这里图片播放器的第一部分介绍完毕,代码非常简陋,欢迎有兴趣的朋友一起改进探讨。往后会继续后后面的部分补充上来。谢谢。

0 0