iOS-iPad照相机得到的照片放入沙盒中再次提取 照片打横

iOS-iPad照相机得到的照片放入沙盒中再次提取 照片打横

虐人心 发布于 2017-02-16 字数 2461 浏览 1011 回复 1

iPad照相机得到的照片放入沙盒中 再次提取照片打横,该怎么办?是不是需要设置图片的什么属性啊

代码如下

-(BOOL) setPhotoToPath:(UIImage *)image isName:(NSString *)name
{
//此处首先指定了图片存取路径(默认写到应用程序沙盒 中)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

//并给文件起个文件名
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:name];
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
if (blHave) {
NSLog(@"already have");
//delete
[self deleteFromName:name];
}
NSData *data = UIImagePNGRepresentation(image);
BOOL result = [data writeToFile:uniquePath atomically:YES];
if (result) {
//NSLog(@"success");
return YES;
}else {
//NSLog(@"no success");
return NO;
}
}
- (UIImage *)getPhotoFromName:(NSString *)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//NSFileManager* fileManager=[NSFileManager defaultManager];
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:name];
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
if (!blHave) {
return nil;
}else
{
NSData *data = [NSData dataWithContentsOfFile:uniquePath];
UIImage *img = [[UIImage alloc] initWithData:data];
// NSLog(@" have");
return img;
}
}

-(BOOL)deleteFromName:(NSString *)name
{
NSFileManager* fileManager=[NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
//文件名
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:name];
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
if (!blHave) {
//NSLog(@"no have");
return NO;
}else {
//NSLog(@" have");
BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
if (blDele) {
//NSLog(@"dele success");
return YES;
}else {
//NSLog(@"dele fail");
return NO;
}
}
}

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜柠檬 2017-09-22 1 楼

相册里的照片是有方向,再根据图片的方向,再反回来就好了!

+ (void)fixOrientation:(UIImage *)aImage {
if (aImage==nil || !aImage) {
return;
}
// No-op if the orientation is already correct
if (aImage.imageOrientation == UIImageOrientationUp) return;

// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
UIImageOrientation orientation=aImage.imageOrientation;
int orientation_=orientation;
switch (orientation_) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;

case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;

case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
}

switch (orientation_) {
case UIImageOrientationUpMirrored:{

}
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;

case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
}

// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage), 0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;

default:
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}

// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
aImage=img;
img=nil;
}