フォトライブラリで選択した画像を回転してリサイズして保存したいです。
Objective-Cで
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; int originalWidth = (int)(image.size.width); int originalHeight = (int)(image.size.height); int w,h,w2,h2; float ratio; if(originalWidth>originalHeight){ ratio=originalHeight/250.0; w=(int)(originalWidth/ratio); h=250; } else{ ratio=originalWidth/250.0; w=250; h=(int)(originalHeight/ratio); } CGImageRef imageRef = [image CGImage]; CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); if (bitmapInfo == kCGImageAlphaNone) { bitmapInfo = kCGImageAlphaNoneSkipLast; } CGContextRef bitmap; bitmap = CGBitmapContextCreate(NULL, w, h, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); w2=w; h2=h; if (image.imageOrientation == UIImageOrientationLeft) { CGContextRotateCTM (bitmap, 90.0*M_PI/180.0); CGContextTranslateCTM (bitmap, 0, -w); w2=h; h2=w; } else if (image.imageOrientation == UIImageOrientationRight) { CGContextRotateCTM (bitmap, -90.0*M_PI/180.0); CGContextTranslateCTM (bitmap, -h, 0); w2=h; h2=w; } else if (image.imageOrientation == UIImageOrientationUp) { // NOTHING } else if (image.imageOrientation == UIImageOrientationDown) { CGContextTranslateCTM (bitmap, w, h); CGContextRotateCTM (bitmap, -180.0*M_PI/180.0); } CGContextDrawImage(bitmap, CGRectMake(0, 0, w2, h2), imageRef); CGImageRef ref = CGBitmapContextCreateImage(bitmap); UIImage *newImage = [UIImage imageWithCGImage:ref]; CGContextRelease(bitmap); CGImageRelease(ref); imageview.image=newImage; NSString *itemPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *str=@"image.jpg"; NSString *filePath = [itemPath stringByAppendingPathComponent:str]; [UIImageJPEGRepresentation(newImage, 0.3f) writeToFile:filePath atomically:YES]; [ipc dismissViewControllerAnimated:YES completion:NULL]; }
このように記述されたコードをswiftにしたいです。特に
if (bitmapInfo == kCGImageAlphaNone) {
でひっかかっています。
全文swiftにしていただければうれしいですが、ポイントとなる部分だけでも構いません。困っています。よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/23 07:39