开发笔记 那些年追过的图片(六):压缩图片并保存到沙盒里

站在巨人的肩膀我们会看见山后面的海


#pragma mark-

#pragma mark 照片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{

    

    

    

    _imageSTring = [Photoimage2String:image];

    imageChange = YES;

    [_login.portraitBtnsetBackgroundImage:imageforState:UIControlStateNormal];

    

    UIImage *headImage = [self imageWithImageSimple:imagescaledToSize:CGSizeMake(160,160)];

    

    [self  saveImageToDocument:headImageimageName:@"image.png"];

    

    //关闭相册界面

    [picker dismissModalViewControllerAnimated:YES];

}



//图片保存到沙盒里


-(void)saveImageToDocument:(UIImage *)image imageName:(NSString *)imagename

{

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *filePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:imagename,nil]];  // 保存文件的名称

    [UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES];

}

//压缩图片

- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

  {

    // Create a graphics image context

    UIGraphicsBeginImageContext(newSize);

    // Tell the old image to draw in this new context, with the desired

    // new size

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    // Get the new image from the context

    UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

    // End the context

    UIGraphicsEndImageContext();

    // Return the new image.

    return newImage;

      }

你可能感兴趣的:(ios,image)