美文网首页
iOS将图片存入到相册中

iOS将图片存入到相册中

作者: c42b9af86268 | 来源:发表于2016-12-28 19:39 被阅读0次

    将图片存入到相册中

    常用李明杰的第三方库。


    UIImageWriteToSavedPhotosAlbum(<#UIImage *_Nonnull image#>, <#id_NullablecompletionTarget#>, <#SEL_Nullable completionSelector#>, <#void * _Nullable contextInfo#>)

    参数:

    image

    带保存的图片UImage对象

    completionTarget

    图像保存至相册后调用completionTarget指定的selector(可选)

    completionSelector

    completionTarget的方法对应的选择器,相当于回调方法,需满足以下格式

    contextInfo指定了在回调中可选择传入的数据

    当我们需要异步获得图像保存结果的消息时,我们需要指定completionTarget对象以及其completionSelector对应的选择器

    - (void)saveImageToPhotos:(UIImage*)savedImage

    {

    UIImageWriteToSavedPhotosAlbum(image,self,@selector(mage:didFinishSavingWiithError:contextInfo:),NULL);

    }

    //指定回调方法

    - (void)image: (UIImage*) image didFinishSavingWithError: (NSError*) error contextInfo: (void*) contextInfo

    {

    NSString*msg = nil ;

    if(error != NULL){

    msg=@"保存图片失败";

    }else{

    msg=@"保存图片成功";

    }

    UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"

    message:msg

    delegate:self

    cancelButtonTitle:@"确定"

    otherButtonTitles:nil];

    [alert show];

    }

    //调用示例

    UIImage*savedImage = [UIImageimageNamed:"savedImage.png"];

    [selfsaveImageToPhotos:savedImage];

    网络数据

    网络下载 使用第三方

    网络上传

    /**

    *图片上传

    */

    -(void)imageViewUpLoad{

    NSData*icon = [[NSDataalloc]initWithBase64EncodedString:@""options:NSDataBase64DecodingIgnoreUnknownCharacters];

    UIImage*image = [UIImageimageWithData:icon];

    NSString*url=@"";

    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFJSONResponseSerializerserializer];

    [manager POST:url parameters:paramsconstructingBodyWithBlock:^(idformData) {

    [formDataappendPartWithFileData:fileData name:@"uploadFile"fileName:@"icon.jpg"mimeType:@"image/jpeg"];

    [MBProgressHUD showMsg:@"正在加载中"];

    } success:^(AFHTTPRequestOperation*operation,idresponseObject) {

    }

    }

    //获取图片并将其存入到沙盒

    -(void)imagePickerController:(UIImagePickerController*)picker

    didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //获取点击的图片

    UIImage*image = info[UIImagePickerControllerOriginalImage];

    self.icon= image;

    NSData*fileData =UIImageJPEGRepresentation(image, 1.0);

    //将图片转化成字符串

    NSString*encodedImageStr = [fileDatabase64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    //存储到沙盒

    ZPEmpInfo*emp = [[ZPEmpInfoalloc]init];

    emp.icon= encodedImageStr;

    [ZPAccountInfoToolsaveEmpIcon:emp];

    [self.iconButtonsetImage:imageforState:UIControlStateNormal];

    [selfdismissViewControllerAnimated:YEScompletion:nil];

    }

    沙盒中:

    #define

    empIcon [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

    NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"empIcon.archiver"]

    //个人头像

    + (void)saveEmpIcon:(ZPEmpInfo*)icon

    {

    [NSKeyedArchiverarchiveRootObject:icontoFile:empIcon];

    }

    + (ZPEmpInfo*)empIconAccount

    {

    ZPEmpInfo*icon = [NSKeyedUnarchiverunarchiveObjectWithFile:empIcon];

    returnicon;

    }

    相关文章

      网友评论

          本文标题:iOS将图片存入到相册中

          本文链接:https://www.haomeiwen.com/subject/dptuvttx.html