圖片壓縮保存處理

 
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize {
    CGSize imageSize = image.size;//取出要壓縮的image尺寸
    CGFloat width = imageSize.width;    //圖片寬度
    CGFloat height = imageSize.height;  //圖片高度
    CGFloat scale = height/width;
    
    // Create a graphics image context
    UIGraphicsBeginImageContext(CGSizeMake(newSize.width,newSize.width *scale));
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.width *scale)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}
//存儲(chǔ)圖像
//在上面我們獲取到了圖片并對(duì)圖片進(jìn)行了壓縮,通過之前的小知識(shí)了解到,將應(yīng)用需要的一些圖片存入沙盒是個(gè)不錯(cuò)的選擇,而且應(yīng)用程序可以直接通過路徑去方法沙盒中的圖片,在這里我們將圖片存入沙盒中的Documents目錄下。

#pragma mark 保存圖片到document
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
}
//從Documents目錄下獲取圖片

//#pragma mark 從文檔目錄下獲取Documents路徑
//- (NSString *)documentFolderPath {
//    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//}
//
////上傳圖片
//
//- (void)upLoadSalesBigImage:(NSString *)bigImage MidImage:(NSString *)midImage SmallImage:(NSString *)smallImage {
//
//}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容