• 用照相机照相并保存上传服务器等操作


    参考:http://www.apple.com.cn/developer/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedFeatures/AdvancedFeatures.html#//apple_ref/doc/uid/TP40007072-CH14-SW13

    .h文件如下:
         #import <UIKit/UIKit.h>
        @interface FirstViewController :      UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate> {//要引入这两个协议
        UIButton *_button;
        UIImagePickerController *_pick;
    }
    -(IBAction)button_click:(id)sender;
    @property(nonatomic,retain)UIImagePickerController *pick;
    @property(retain,nonatomic)IBOutlet UIButton *button;
    @end

    .m文件如下:

    //保存图片到Document
    - (void)saveImage:(UIImage *)tempImage{
        NSData* imageData = UIImagePNGRepresentation(tempImage);
        NSString* imageNameStr = @"1.png";
       
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* documentsDirectory = [paths objectAtIndex:0];
        // Now we get the full path to the file
        NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageNameStr];
        // and then we write it out
        [imageData writeToFile:fullPathToFile atomically:NO];
       
    }
    -(IBAction)button_click:(id)sender{
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            return;
        }
        self.pick = [[UIImagePickerController alloc] init];
        self.pick.delegate = self;
       // self.pick.sourceType = UIImagePickerControllerSourceTypeCamera;//调用相机
        self.pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//调用手机图片库
        self.pick.allowsEditing = NO;//是否开启图片编辑功能
        [self presentModalViewController:self.pick animated:YES];
    }

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
         //image表示用相机拍摄的原始图片或是从图片库选择的图片,没有经过编辑的
         //editingInfo表示经过编辑的图片信息,若图片编辑功能没开启,此值为nil
        //其key值说明如下:
       //URL:  https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/doc/constant_group/Editing_Information_Keys

        //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//iOS-将图片保存到照片相册中   
        [self saveImage:image];
        [[self.pick parentViewController] dismissModalViewControllerAnimated:YES];
        [self.pick release];
    }
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [[self.pick parentViewController] dismissModalViewControllerAnimated:YES];
        [self.pick release];
    }


    相关说明:
    iOS-将图片保存到照片相册中
         
    UIImageWriteToSavedPhotosAlbum(drawImage.image, nil, nil, nil);
    

    这里drawImage是一个UIImageView。

    为nil的参数,可在UIKit Function Reference中查到。UIImageWriteToSavedPhotosAlbum是UIKit框架中的一个函数。

    这里说一下后面三个参数的含义:

    1
    2
    3
    4
    5
    6
    
    void UIImageWriteToSavedPhotosAlbum (
       UIImage  *image,
       id       completionTarget,
       SEL      completionSelector,
       void     *contextInfo
    );
    

    id是target对象,sel是selector,即target对象上的方法名,contextInfo是任意指针,会传递到selector定义的方法上。一般是当完成后调用方法时使用,或者在完成时出错的处理。


    图片上传至服务器:客户端采用ASIHttpRequest.h这个开源库

    服务器端:(php)

    <?php //upload即是app那边 setFile 函数中后面的那个forKey参数
    if (true)
      {
      if ($_FILES["upload"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
        echo "Type: " . $_FILES["upload"]["type"] . "<br />";
        echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
        echo "Temp upload: " . $_FILES["upload"]["tmp_name"] . "<br />";

        if (file_exists($_FILES["upload"]["name"]))
          {
          echo $_FILES["upload"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["upload"]["tmp_name"],
          $_FILES["upload"]["name"]);
          echo "Stored in: " .$_FILES["upload"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid upload";
      }
    ?>

    客户端:

    NSURL *url = [NSURL URLWithString:@"http://192.168.10.199/tmart/app_file.php?file=upload"];
        ASIFormDataRequest *req = [ASIFormDataRequest requestWithURL:url];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [paths objectAtIndex:0];
        NSString *filename = [path stringByAppendingPathComponent:@"tmart.sql"];
        [req setFile:filename forKey:@"upload"];
        [req setDelegate:self];
        [req setDidFinishSelector:@selector(suc:)];
        [req startSynchronous];


  • 相关阅读:
    《编写可维护的JavaScript》之编程实践
    CSS基础知识之float
    tab.js分享及浏览器兼容性问题汇总
    CSS基础知识之position
    样式布局分享-基于frozen.js的移动OA
    DoNet开源项目-基于jQuery EasyUI的后台管理系统
    DoNet开源项目-基于Amaze UI的点餐系统
    DSOframer 微软官方API的查阅方法
    DSOframer 的简单介绍和资源整理
    2015-新的阻力,新的努力
  • 原文地址:https://www.cnblogs.com/cnsec/p/11515898.html
Copyright © 2020-2023  润新知