objective-c 图片处理机制

ImageProcessing.h 文件

----------------------------------------------------

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface ImageProcessing : NSObject

{

    UIImage* newImage;

}

@property(nonatomic,strong)NSMutableArray * imageMuarray;

-(UIImage*)imageWithImage:(UIImage*)image;

-(void)GCDdemoRunArray:(NSArray * )array;


@end

----------------------------------------------------------------------------------------------

ImageProcessing.m 文件

#import "ImageProcessing.h"


@implementation ImageProcessing

//对照片进行压缩

-(UIImage*)imageWithImage:(UIImage*)image

{

    newImage = image;

    //获取图片字节

    NSData * imagedataI=UIImageJPEGRepresentation(image, 1.0);

    NSLog(@"data:%lu",imagedataI.length);

    

    while(imagedataI.length >100000) {

        newImage=[self changeImage:image size:2];

        image =newImage;

        imagedataI=UIImageJPEGRepresentation(image, 1.0);

        NSLog(@"1");

    }

    

    return newImage;


}

-(UIImage *)changeImage:(UIImage*)image size:(int)num{

    CGSize  size =image.size;

    //设置图片的尺寸

    size.width=image.size.width/num;

    size.height=image.size.height/num;

    //对图片尺寸进行压缩

    UIGraphicsBeginImageContext(size);

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

    UIImage* newImageOne = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

   

    return newImageOne;

}


-(NSMutableArray * )imageMuarray{

    if (_imageMuarray==nil) {

        _imageMuarray=[NSMutableArray array];

    }

    return _imageMuarray;

}

//串行对列

-(void)GCDdemoRunArray:(NSArray * )array{

    dispatch_queue_t serialQueue=dispatch_queue_create("mySerialQueue", DISPATCH_QUEUE_SERIAL);

    //创建一个线程

    for (int i=0; i<array.count; i++) {

        //创建异步执行对列

        //创建串行队列所以不会开启新的线程

        dispatch_async(serialQueue, ^{

            [self compressPicArray:array Index:i];

            

        });

    }

}

-(void)compressPicArray:(NSArray *)newarray Index:(int)index{


    UIImage * newimage= [self imageWithImage:newarray[index]];

    [self.imageMuarray addObject:newimage];

    NSData *data = UIImageJPEGRepresentation(_imageMuarray[index],1);

    [data writeToFile:[NSString stringWithFormat:@"/Users/dc017/desktop/ok%i.png",index] atomically:YES];

    NSLog(@"%@",_imageMuarray);

    

}

@end


你可能感兴趣的:(objective-c 图片处理机制)