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

本文介绍了作者在学习iOS开发过程中制作的一个简易图片播放器Demo,包括从手机相册选取图片、拍照获取图片以及通过网络URL加载图片等功能的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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


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


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

 

         实现的初始思路大概是:


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


//一下是整个button的代码

    

- (IBAction)ScanButton:(id)sender {

    UIActionSheet *sheet;


    // 判断是否支持相机


    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])


    {

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

        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];


    }


    else {


        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" 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([UIImagePickerController isSourceTypeAvailable: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 = [[UIImagePickerController alloc] init];


        imagePickerController.delegate = self;


        imagePickerController.allowsEditing = YES;


        imagePickerController.sourceType = sourceType;


        [self presentViewController:imagePickerController animated: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:YES completion:^{}];


    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    


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

    

    [self saveImage:image withName:@"currentImage.png"];


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


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


    _isFullScreen = NO;


    [imageArray addObject:savedImage];

    

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

    

    [imageView setImage:[imageArray objectAtIndex:i]];


    front = i;

    

    i++;

    

    

    imageView.tag = 100;


}




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


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

  

         

- (IBAction)SureButton:(id)sender {

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

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

    

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

    

    

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

    

    NSData *data = [NSData dataWithContentsOfURL:url];

    

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

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

    [self.imageView setImage:image];

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

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

    front = i;

    i++;


    [urlappear setText:PictureUrl.text];

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

}

         


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值